C + + exception handling

Source: Internet
Author: User

Exceptions are thrown and handled using the following three keywords: try, throw, catch. Throwing an exception is the detection of an exception, which is implemented in C + + with the throw statement, and throws an exception if an exception is detected. If an exception is found in the program segment of the TRY statement block, including the function in which it was called, and the exception is discarded, the exception can be caught and processed by a catch statement after the try statement block, and the condition that is caught and processed is that the type of the thrown exception matches the exception type of the catch statement. Because C + + uses data types to differentiate between different exceptions, the value of the expression in the throw statement has no practical significance when judging the exception, and the type of the expression is particularly important. Therefore, the parameters of the catch block can have no parameter names, only the parameter types are required. If the thrown exception has no function catch (catch), it will be uploaded to the C + + runtime system, causing the entire program to terminate.
The resource can normally be freed after an exception is thrown, but note that if an exception is thrown in the class's constructor, the system does not call its destructor, and the process is: If an exception is thrown in the constructor, remember to delete the requested resource before throwing it. The parameters of a catch block are recommended for address passing instead of value passing, which not only improves efficiency, but also makes use of the polymorphism of the object. In addition, exception captures for derived classes are placed before the parent class exception captures, otherwise the exception of the derived class cannot be caught. When writing an exception description, ensure that the exception description for the derived class member function is consistent with the exception description of the base class member function.

A simple example:

#include <iostream>using namespacestd;/*If an exception is found inside a try{} block, an exception is thrown by the throw statement, and the catch statement catches the exception*/voidMain () {DoubleA, B, C; cout<<"Enter a, B:"; CIN>> a >>b; Try    {        if(b = =0)            ThrowC//Throw Exceptionc = A/b; }    Catch(Double)//Catching exceptions{cout<< Endl <<"the divisor cannot be 0! "<<Endl; Exit (1); } cout<<"A/ B ="<< C <<Endl;}

You can list all the exception types that this function might throw in the declaration of a function:

void Fun () throw (a,b,c,d);

This function can throw any type of exception:

void Fun ();

Functions that do not throw any type of exception can be declared in the following form:

void Fun () throw ();

#include <iostream>#include<string>using namespacestd;//Custom Exception Classesclassdividezero{ Public: Dividezero (stringmsg) {         This->msg =msg; }    voiddisplay () {cout<< msg <<Endl; }Private:    stringmsg;};intTestintAintb) {intC; if(b = =0)        ThrowDividezero ("Divisor is 0"); C= A/b; returnC;}intMain () {intC; Try{C= Test (3,0); }    Catch(Dividezero e) {e.display (); } cout<< C <<Endl;}

C + + exception handling

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.