/******************************************************************************/ /* */ /* FILE: july.cpp */ /* */ /* Another strange aspect of C++ exception handling */ /* ================================================ */ /* */ /* Compiled and tested with g++ 3.2.3 20030415 */ /* */ /* V1.00 31-JUL-2004 P. Tellenbach http://www.heimetli.ch/ */ /* */ /******************************************************************************/ #include <iostream> #include <exception> #include <cstdlib> using namespace std ; void handler( ) { try { throw ; } catch( const char *ps ) { cout << ps << endl ; } exit( 0 ) ; } void func() throw( ) { throw "Hello world !" ; } int main() { set_unexpected( handler ) ; func() ; return 0 ; }
Update 31. July 2024
set_unexpected was removed from the standard with C++ 17, and throw() was deprecated. Therefore the code was updated to conform to the current standard.
/******************************************************************************/ /* */ /* FILE: july.cpp */ /* */ /* Another strange aspect of C++ exception handling */ /* ================================================ */ /* */ /* Compiled and tested with g++ 3.2.3 20030415 */ /* */ /* V1.00 31-JUL-2004 P. Tellenbach https://www.heimetli.ch */ /* V2.00 31-JUL-2024 P. Tellenbach https://www.heimetli.ch */ /* */ /******************************************************************************/ #include <iostream> #include <exception> #include <cstdlib> using namespace std ; void handler( ) { try { throw ; } catch( const char *ps ) { cout << ps << endl ; } exit( 0 ) ; } void func() noexcept { throw "Hello world !" ; } int main() { set_terminate( handler ) ; func() ; }