/*************************************** ****************************************: Exceptions. cpp * function: Exception Handling for learning C ++ Premier Notes * Description: throwing a custom exception class Object throws a built-in type object (such as int). Although C ++ supports exceptions, however, in the C ++ program, try to use other error handling techniques (C ++ Premier) * OPERATOR: JarvisChu: create ************************************** **************************************** * ******************/# include <iostream> # include <string> using Namespace std; # define TYPE_CLASS 0 // throw an exception for a custom Class Object # define TYPE_INT 1 // throw an integer exception # define TYPE_ENUM 2 // throw an enumeration exception # define TYPE_FLOAT 3 // throw float exception # define TYPE_DOUBLE 4 // throw the double exception typedef int TYPE; // exception type enum Week {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday}; // custom exception class MyException {public: MyException (string msg) {err_msg = msg;} void ShowErrorMsg () {cerr <err_msg <endl ;}~ MyException () {} private: string err_msg;}; // the function that throws an exception // throw (MyException, int, Week) is called the exception specification, // It tells the compiler, this function does not throw other types of exceptions. // The exception specification can be left blank. By default, any types of exceptions can be thrown. // if an exception is not caught, the system calls terminate for processing. // If there is no writing exception specification for an exception TYPE, we cannot catch it and it will be captured by the system. Call terminatevoid KindsOfException (type TYPE) throw (MyException, int, Week, float, double) {switch (type) {case TYPE_CLASS: throw MyException ("Exception! Type of Class "); // Class break; case TYPE_INT: throw 2011; // integer break; case TYPE_ENUM: throw Monday; // enumeration break; case TYPE_FLOAT: throw 1.23f; // float break; case TYPE_DOUBLE: throw 1.23; // double break; default: break;} int main () {int type; cout <"Input the type, 2, 3, 4): "; cin> type; try {KindsOfException (type);} catch (MyException e) {// If the throw exception specification is used, however, MyException is not written to the throw list. e. showErrorMs G (); // If the MyException exception is still not caught here, it will be handled by the System Call terminate.} Catch (float f) {cerr <"float" <f <endl;} catch (double d) {cerr <"double" <d <endl;} catch (int I) {cerr <"Exception! Type of Int --> "<I <endl;} catch (Week week) {cerr <" Exception! Type of Enum --> "<week <endl ;}// more catch statements can be found: return 0 ;}