Difference between exception handling in C++ and Java?

前端 未结 5 1893
孤城傲影
孤城傲影 2021-02-04 00:45

In Java, if a specific line of code causes the program to crash, then the exception is caught and the program continues to execute.

However, in C++, if I have a piece of

5条回答
  •  忘掉有多难
    2021-02-04 01:21

    Not all crashes are due to unhandled exceptions. For your example, the C++ standard says that dereferencing the NULL pointer results in undefined behaviour. In Windows, you can handle problems that crash your program without throwing a C++ exception with structured exception handling (SEH): __try/__except/__finally. In Unix, you can set up special signal handlers.


    Also, there is an error in your code. The exception handler for const char * would only be invoked when an exception of this type is thrown. For standard exceptions, you should catch std::exception or it's appropriate subclasses. To catch any C++ exception, use catch (...).

提交回复
热议问题