Skip to content

Instantly share code, notes, and snippets.

@bols-blue
Created May 30, 2017 10:02
Show Gist options
  • Save bols-blue/c5696042491eb94da57ac7fb33dcf142 to your computer and use it in GitHub Desktop.
Save bols-blue/c5696042491eb94da57ac7fb33dcf142 to your computer and use it in GitHub Desktop.
C++ exeption sample
#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