Created
May 30, 2017 10:02
-
-
Save bols-blue/c5696042491eb94da57ac7fb33dcf142 to your computer and use it in GitHub Desktop.
C++ exeption sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <stdexcept> | |
using namespace std; | |
class my_exception_t : public exception | |
{ | |
const char * str; | |
public: | |
int data = 10; | |
int test2(){ | |
return data; | |
} | |
explicit my_exception_t(char * c) | |
{ str = c; } | |
virtual const char* what() const throw() | |
{ return "Hello, world!"; } | |
const char * hello() throw(){ | |
return "hello"; | |
} | |
const char * test() throw(){ | |
return str; | |
} | |
}; | |
int main() | |
{ | |
my_exception_t ex = my_exception_t("test: "); | |
ex.data = 20; | |
ex.test2(); | |
try { | |
throw ex; | |
} catch (my_exception_t& error){ | |
cerr << "Exception: " << error.what() << endl; | |
cerr << error.hello() << endl; | |
cerr << error.test() << error.data << endl; | |
} catch (exception& error){ | |
cerr << "Exception: " << error.what() << endl; | |
} catch (...) { | |
cerr << "Exception: unknown" << endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment