|
LCK Exception Handling
|
Exercising C++ exception handling.
:arrow_forward: return to the Catalog
The essentials of exception handling in c++:
catch blocks allow catching different types of errorsthrow(), which is deprecated in C++11exception_handling.cpp runs through a variety of exception classes that are caught:
..and ends on an uncaught exception.
NB: this is updated for C++17 syntax - specifically the use of noexcept instead of throw().
$ g++ -std=c++17 -o exception_handling.exe exception_handling.cpp && ./exception_handling.exe Testing a range of standard and custom exceptions.. Error code: 33 Basic error message: Oh deary me String error message caught by reference: Out of bacon bad_alloc error message caught by reference: std::bad_alloc Generic exception error message caught by reference: std::exception CustomException message caught by reference: Something bad happened Now testing an exception in a constructor.. exception_handling.exe(28207,0x7fffae28f380) malloc: *** mach_vm_map(size=1000000000000000) failed (error code=3) *** error: can't allocate region *** set a breakpoint in malloc_error_break to debug bad_alloc message: std::bad_alloc And we're still running! But the next has no handler: libc++abi.dylib: terminating with uncaught exception of type int Abort trap: 6
1.8.14