Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Throw restrictions on an exception handling function
- // practice...
- #include <iostream>
- #include <conio.h>
- #include <stdlib.h>
- #include <exception>
- using namespace std;
- void mr_terminator(void) {
- cout << "Exception could not be handled." << endl;
- exit(0);
- }
- void test(int x) throw(int,double) {
- if (x == 0) throw 'x'; //char
- else if (x == 1) throw x; //int
- else if (x == -1) throw 1.0; // double
- cout << "Exception could not be found." << endl;
- }
- int main(void) {
- set_terminate(mr_terminator);
- try {
- cout << "Invoking Restricted Throw Function." << endl;
- cout << "x == 0" << endl;
- test(0);
- cout << "x == 1" << endl;
- test(1);
- cout << "x == -1" << endl;
- test(-1);
- cout << "x == 2" << endl;
- test(2);
- }
- catch (char c) {
- cout << "Caught a character." << endl;
- }
- catch (int m) {
- cout << "Caught an integer." << endl;
- }
- catch (double d) {
- cout << "Caught a double." << endl;
- }
- catch (...) {
- cout << "Caught an exception." << endl;
- }
- cout << "End of the Program." << endl;
- getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement