The implementation method of C + + throw exception

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

The C + + language, like other programming languages, contains processing of exceptions. We will be here for you to explain in detail about C + + Throw exception implementation methods, and the application of the exception. I hope you can get some help to improve your understanding of this.

C + + throws an exception (also known as a discard exception) that detects whether an exception is generated, in C + +, it is implemented with 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.

Handle 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 to C + + to implement exception handling, as shown in the code listing.

  #include <iostream.h>//Include header file #include <stdlib.h> double fuc (double x, double y)//define function

{if (y==0)

{throw y;//divisor 0, throw exception}

return x/y; Otherwise return two number of quotient}

void Main ()

{double res; try//Definition exception

  {res=fuc (2,3);  cout<< "The result of x/y is:" <<res<<endl; RES=FUC (4,0); An exception occurred

}  

catch (double)//catch and handle exceptions

{cerr<< "error of dividing ZERO.N"; exit (1);//Abnormal Exit program}

}

Run results creates a new C + + source file in Visual C + +, entering the code above, and running after compiling.

In the preceding code of "Example resolution", the try statement is used to define an exception in line 14th to 19th of main function main (), which contains 3 statements that are likely to have exceptions, and they are functions that divide by two numbers. The exception handling is defined in line 20th to 24th of the code, that is, when an exception is caught, the statement in that section of code is executed. In addition, the code 5~8 line in the function fuc () implements C + + throw an exception through the throw statement.

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.