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

836 B
Raw Blame History

exception

  • 基本结构try-catch

    try{ expression }catch(errorType err){ expression }catch(errorType err){ expression }...

  • 文件支持

    #include //#include 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 }