C\C++ uses the exception class, throws a custom exception and catches it

Author of the article: Caspian
Source Website: https://blog.csdn.net/WangPaiFeiXingYuan


Introduction:

Throw an exception, and catch the exception

Effect:

      

code:

#include <iostream>  
#include <exception>  
#include <stdexcept>  

int main() 
{
    try 
    {
        // 抛出一个自定义异常  
        throw std::runtime_error("错误!");
    }
    catch (const std::exception& ex) 
    {
        std::cout << "捕获异常: " << ex.what() << std::endl;
    }
    catch (...)
    {
        std::cout << "..." << std::endl;
    }

    std::cin.get();
    return 0;
}

Guess you like

Origin blog.csdn.net/WangPaiFeiXingYuan/article/details/131803856