Skip to content

Instantly share code, notes, and snippets.

@danthemango
Created February 21, 2018 04:42
Show Gist options
  • Save danthemango/65bce895c54f96bf6b7c0cf8cd26a8f3 to your computer and use it in GitHub Desktop.
Save danthemango/65bce895c54f96bf6b7c0cf8cd26a8f3 to your computer and use it in GitHub Desktop.
Minimal C++ Exceptions with exception class
// example program which throws a class exception
#include <iostream>
using namespace std;
// my error class
class WrongFormat{
public:
string what(){
return string("Wrong Format!");
}
};
int main(){
try{
// oh no, something bad happened
throw WrongFormat();
}catch(WrongFormat w){
cout << w.what() << endl;
}
}
// example program which just throws a string exception
#include <iostream>
using namespace std;
int main(){
try{
// oh no, something bad happened
throw string("something happen");
}catch(string s){
cout << "Error: " << s << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment