Files
912-notes/c++ note/exception.md
2019-05-21 14:02:50 +08:00

38 lines
836 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
## exception
+ 基本结构`try-catch`
try{
expression
}catch(errorType err){
expression
}catch(errorType err){
expression
}...
+ 文件支持
#include <stdexcept>
//#include <exception>
using std::runtime_error
+ 具体使用方法
- try block内放可能会产生异常的语句并抛出(throw)相应的异常
try{
if(...) throw runtime_error("fatal error: dividing zero");
if(...) throw ...
//else
normal expression
}
- 在catch中检查可能会出现的异常若异常与catch的异常类型对应则进入相应的异常处理程序
try{
...
}catch(runtime_error err){
cout << err.what() << endl;//err.what()返回抛出异常时初始化的字符串
//other handling
cout << "continue or not(y/n): ";
char c;
cin >> c;
if(c == 'n' || c == 'N') break;
//else continue
}