C ++ Exception Handling instance

Source: Internet
Author: User

/*************************************** ****************************************: 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 ;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.