C++ Exceptions - Is throwing c-string as an exception bad?

前端 未结 7 2196
花落未央
花落未央 2020-12-09 15:17

I am working on a small c++ program and learning exceptions. Is the following code \"bad\", and if so, what can I do to improve it?

try {
    // code
    if          


        
7条回答
  •  一个人的身影
    2020-12-09 15:43

    I think this is much more simpler. :).

    #include 
    #include 
    
    using namespace std;
    
    int main() {
        try {
            throw runtime_error("This is an Error"); 
        }catch (exception& e){
            cout << "Exception: " << e.what() << endl; 
        }
        return 0; 
    }
    

提交回复
热议问题