C + + exception handling mechanism

Source: Internet
Author: User
Tags define function exception handling function prototype

C + + exception handling mechanism

The C + + exception handling mechanism is a very powerful and flexible tool to effectively handle running errors, providing more flexibility, security, and robustness, overcoming the problems of traditional methods.

The throwing and handling of exceptions mainly uses the following three keywords: try, throw, catch.

Throwing an exception detects whether an exception is generated, in C + +, it is implemented using a throw statement, and if an exception is detected, an exception is thrown. The format of the statement is:
Expression of throw;
If an exception is found in the program segment of the TRY statement block, including the function called in it, and discarding the exception, the exception can be caught and handled by a catch statement after the try statement block, and the condition that the thrown exception is the type that matches the exception type of the catch statement. Because C + + uses data types to differentiate between exceptions, the value of an expression in a throw statement is not meaningful when judging an exception, and the type of the expression is particularly important.

The Try-catch statement forms the following:


Try
{
Contains statements that may throw exceptions;
}
catch (type name [formal parameter name])//Catch a specific type of exception
{

}
catch (type name [formal parameter name])//Catch a specific type of exception
{

}
catch (...) Three dots that capture all types of exceptions
{
}



"Example 1" handles an exception with a divisor of 0. This example uses the Try/catch statement to catch an exception with a 0 exception, and use the throw statement to throw an exception, which enables exception handling, as shown in listing 1-1 of code.
Code Listing 1-1

1 #include <iostream.h>//Include header file
2 #include <stdlib.h>

3 double fuc (double x, double y)//define function
4 {
5 if (y==0)
6 {
7 Throw y; Divisor is 0 and throws an exception
8}
9 return x/y; Otherwise return two number of quotient
10}

one void Main ()
12 {
Double res;
Try//Definition exception
15 {
RES=FUC (2,3);
cout<< "The result of x/y is:" <<res<<endl;
RES=FUC (4,0); An exception appears, and an exception is thrown inside the function
19}
catch (double)//catch and handle exceptions
21 {
cerr<< "error of dividing zero./n";
To exit (1); Exception Exit Program
24}
25}


"Example 2" Custom exception type (demonstrated in the code at the beginning of this article)

Third, the interface declaration of the exception

To enhance the readability of the program so that users of the function can easily know what exceptions are thrown by the function they are using, you can list all the exception types that the function might throw in the declaration of the function, for example:

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

This indicates that the function fun () May and only throw the exception of the type (A,B,C,D) and its subtypes.

If an interface declaration for an exception is not included in the declaration of a function, this function can throw any type of exception, for example:

void Fun ();


A function that does not throw out any type of exception can declare the following form:

void Fun () thow ();

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP
V. Issues to be noted in exception handling

1.  If an exception is thrown that has no function capture (catch), it is uploaded to C + + Run the system there, causing the end of the entire program

2.  The resource can normally be freed after an exception is thrown, but note that if an exception is thrown in the constructor of the class, the system does not call its destructor, and the treatment is that if an exception is thrown in the constructor, Remember to delete the requested resource before you throw it.

3.  exception handling is matched only by type rather than by value, so the parameters of the catch block can have no parameter name, only the parameter type. The description of the exception in the

4.  function prototype is consistent with the description of the exception in the implementation, or it can easily cause an exception conflict.

5.  should write an exception object after the throw statement, throw first constructs a new object through the copy constructor and passes the new object to  catch. ,
then how the new object is freed when the exception is thrown. The
exception handling mechanism guarantees that the new object thrown by the exception is not created on the function stack, but is created on a dedicated exception stack, so that it can be passed across multiple functions to the upper level or destroyed during stack emptying. The destructor of all objects constructed from try to throw statements is automatically invoked. However, if a matching catch block has not been found since the main function has been traced, the system call terminate () terminates the entire program, which does not guarantee that all local objects will be destroyed correctly.

6. The parameters of the catch block recommend address delivery rather than value transfer, not only to improve efficiency, but also to take advantage of object polymorphism. In addition, the exception flutter of the derived class is to be placed in front of the parent class exception, otherwise the exception of the derived class cannot be caught.

7.  When writing an exception description, make sure that the exception description of the derived class member function is consistent with that of the base class member function, that is, the exception description of the virtual function overridden by the derived class should be at least the same as the exception description of the corresponding base class virtual function, even stricter and more special

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.