1// using standard exceptions
2#include <iostream>
3#include <exception>
4using namespace std;
5
6class myexception: public exception {
7 virtual const char* what() const throw() {
8 return "My exception happened";
9 }
10} myex; // declare instance of "myexception" named "myex"
11
12int main () {
13 try {
14 throw myex; // alternatively use: throw myexception();
15 } catch (exception& e) { // to be more specific use: (myexception& e)
16 cout << e.what() << '\n';
17 }
18 return 0;
19}