Skip to content

Instantly share code, notes, and snippets.

@aHisayoshiSuehiro
Forked from bols-blue/exeption.cc
Last active May 30, 2017 10:29
Show Gist options
  • Save aHisayoshiSuehiro/2210e8312c828ce65251c027d6060106 to your computer and use it in GitHub Desktop.
Save aHisayoshiSuehiro/2210e8312c828ce65251c027d6060106 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