Implementation of C ++ throwing an exception

Source: Internet
Author: User

Implementation of C ++ throwing an exception

The C ++ language is the same as other programming languages, including exception handling. Here we will explain in detail how to implement C ++ throw exceptions and how to apply them. I hope you can get some help to improve your understanding of this.

C ++ throws an exception (also known as throwing an exception) to check whether an exception is generated. In C ++, it uses a throw statement. If an exception is detected, an exception is thrown. The statement format is:

Throw expression;

If an exception is found in the program segment of the try statement block (including the called function) and the exception is discarded, the exception can be captured and processed by a catch statement after the try statement block. The condition for capturing and processing is that the type of the discarded exception matches the exception type of the catch statement. C ++ uses data types to identify different exceptions. Therefore, when an exception is identified, the expression value in the throw statement has no practical significance, and the expression type is particularly important.

Handle the exception where the divisor is 0. This example uses the try/catch statement to capture the exception with the preceding Division 0, and throw the exception in C ++ to handle the exception, the implementation code is shown in the code list.

# Include <iostream. h> // contains the header file # include <stdlib. h> double fuc (double x, double y) // defines the Function

{If (y = 0)

{Throw y; // If the divisor is 0, an exception is thrown}

Return x/y; // otherwise, two vendors are returned}

Void main ()

{Double res; try // definition exception

{Res = fuc (); cout <"The result of x/y is:" <res <endl; res = fuc (); // exception

}

Catch (double) // catch and handle exceptions

{Cerr <"error of dividing zero. n"; exit (1); // unexpectedly exits the program}

}

[Running result] Create a New [C ++ Source File] File in Visual C ++, enter the above Code, compile it correctly, and run it.

[Example resolution] in the above Code, the main function main () 14th ~ Row 19 uses the try statement Definition exception, which contains three statements that may encounter exceptions. They call two functions with division of numbers. In code 20th ~ Line 24 defines exception handling, that is, to execute the statements in this section of code after capturing the exception. In addition, the Code in function fuc () is 5 ~ Row 8 throws an exception in C ++ through the throw statement.

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.