zoukankan      html  css  js  c++  java
  • try/throw/catch

    try 保护代码,throw抛出值,catch接受并处理异常

    一般格式

    try
    {    
    //程序中抛出异常
        throw value;
    }
    
    catch(valuetype v)
    {    
    //异常处理程序
    
    }
    View Code

    测试示例

    #include <iostream>
    
    using namespace std;
    
    int main(int argc,char *argv[])
    {
    
        cout<< "In main"<<endl;
        //Define a try block, which is code block enclosed  by a pair of curly braces {} 
        try{
            cout<< "In the try block being ready to throw an exception"<<endl;
            //Since code being protected in the try block,so after the exception is thrown, the program  control flow will go to the next catch block.
            throw 1;
            cout<<"In the try block, because  throws an exception, so  the code here is not going to be executed"<<endl;
    
        }
        //Here must correspond to the definition of at least one catch block, the same it is also enclosed in curly braces
        catch(int & value)
        {
            cout<< "In the catch block,  handling the exception errors. the  exception value is  "<<value<<endl;
            cout<< "Backing into main,execution resumes here."<<endl;
    
        }
    
        cout<<"Testing finished!"<<endl;
        return 0;
    }
    View Code
  • 相关阅读:
    模板为webpack的目录结构
    实例和内置组件
    微信小程序之富文本解析
    微信小程序获取输入框(input)内容
    for循环的语法和执行顺序
    循环
    选择结构(二)
    选择结构
    算术运算
    变量
  • 原文地址:https://www.cnblogs.com/gjianw217/p/3702360.html
Copyright © 2011-2022 走看看