+ -
当前位置:首页 → 问答吧 → c++ 异常exception这部分看了半天, 不明白到底什么意思啊

c++ 异常exception这部分看了半天, 不明白到底什么意思啊

时间:2011-12-13

来源:互联网

http://www.cplusplus.com/doc/tutorial/exceptions/( E0 @6 ]9 A' u1 c3 H  K( A
这代码到底是干嘛的?
复制内容到剪贴板
代码:
#include <iostream>
using namespace std;
int main ()
{ try { throw 20; }

catch (int e) {
cout << "An exception occurred. Exception Nr. " << e << endl; }
return 0;}
[ 本帖最后由 alleni 于 2011-12-13 06:16 编辑 ]

作者: alleni   发布时间: 2011-12-13

Exceptions provide a way to react to exceptional circumstances (like runtime errors) in our program by transferring control to special functions called handlers./ z4 }% b+ b8 D" P
2 S: g  n' A/ @% _  P, q
To catch exceptions we must place a portion of code under exception inspection. This is done by enclosing that portion of code in a try block. When an exceptional circumstance arises within that block, an exception is thrown that transfers the control to the exception handler. If no exception is thrown, the code continues normally and all handlers are ignored.7 b5 P: R% }$ E& b6 x0 v

7 w9 F8 n6 H% m8 N& J. ]An exception is thrown by using the throw keyword from inside the try block. Exception handlers are declared with the keyword catch, which must be placed immediately after the try block:" q( j$ f) J( F+ \1 t3 `1 E
英文你应该比我学得好,我就不多解释了;  O$ f4 E' j* b! \6 s1 m
try中掏出异常,然后把控制权转给句柄(handle,应该是这样翻译)执行相应语句

作者: 月夜幻影   发布时间: 2011-12-13