c/c++ Exceptions

来源:互联网 发布:网络虚拟家庭 编辑:程序博客网 时间:2024/06/13 22:52
Exceptions provide a way to react to exceptional circumstances (like runtime errors) in programs by transferring control to special functions called handlers.

To catch exceptions, a portion of code is placed 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.

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:

123456789101112131415
// exceptions#include <iostream>using namespace std;int main () {  try  {    throw 20;  }  catch (int e)  {    cout << "An exception occurred. Exception Nr. " << e << '\n';  }  return 0;}
An exception occurred. Exception Nr. 20

0 0