c++--Exception Handling

Source: Internet
Author: User

Objective

Large and very complex programs tend to produce some hard-to-find, even unavoidable, run-time errors. When a run-time error occurs, the program cannot simply end up running, but instead falls back to the beginning of the task, indicating the error, and the user decides what to do next. The object-oriented exception handling (exception handling) mechanism is a powerful tool for the C + + language to solve this problem. When the function executes, any type of data object that is placed in the try (test) block can be thrown by a throw block and then returned along the call chain until caught by a catch block, where exception handling is performed, the exception is reported, and so on. From the throw to the capture, each nested call function should be removed from the stack of automatic objects, automatic variables, and field-protected content. The entry function is terminated by abort () if the Exit function has not yet been captured.

The exception (exception) described here is what the program might detect, run-time anomalies, such as storage space exhaustion, array out-of-bounds, being 0, and so on, to anticipate where it might occur, but not exactly how and when it happened. Especially in a large-scale program (software), the parts of the program are written by different groups, which are connected by public interfaces, errors may occur in mutual coordination, and may occur in a combination of individual conditions that could not have been previously imagined.

C + + provides some built-in language features to produce (raise) or throw (throw) exceptions to notify "Exceptions have occurred," and then to catch (catch) exceptions by pre-arranged fields and process them. This mechanism allows for "exception" communication in two unrelated (often independently developed) parts of a C + + program. Another part of the exception is thrown by one part of the program, which can go back to the part that caused the exception to be processed (along the call chain of the program function). It is also a good way to distinguish between responsibilities.

Exception specification

The C + + function has 2 return values, the left side is normal return, and the right side is the exception return

float Div (int a,int b) default is to throw all exceptions

The float Div (int a,int b) throw (int,short,test) specifies that only exceptions of type int, short, Test can be thrown. If it is a float type exception, it cannot be thrown

Mechanism of exception handling

First, exceptions are often implemented in C + + with classes (Class), and in the case of stacks, the exception class is declared as follows:
Class poponempty{...}; Stack NULL exception
Class pushonfull{...}; Stack Full exception

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.