C ++ "Exception Handling", Exception Handling

Source: Internet
Author: User

C ++ "Exception Handling", Exception Handling
Exception ConceptI. Syntax ErrorDuring programming, the keywords in the program are misspelled, the identifiers are undefined, the control structure is incomplete, and the program statements do not conform to the compiler's syntax rules, this error is indicated by the table compiler during compilation and connection.
Ii. logical errorsThe programmer's lack of understanding of the problem may lead to incorrect algorithm design. As a result, although the program can run smoothly, it does not get the expected structure. Such errors are found through debugging and testing.
Iii. Running ErrorDuring the program running process, unexpected results may cause abnormal program planting due to running environment problems, such as insufficient memory space, the file opened does not exist, the file cannot be read or written, and the operation except 0 is executed.


C ++ Exception Handling MechanismI. Exception HandlingThe exception handling in C ++ introduces three keywords: try (exception detection), throw (throw exception), and catch (catch exception). This structured form is used to describe the exception handling process.

Ii. Exception Handling Functions1. try is responsible for monitoring abnormal program segments. When an exception occurs in the program running, it will detect this exception and the program will no longer be able to move forward along the normal Program Logic Path.
2. throw throws an exception and gives control of the program to the catch clause.
3. catch is responsible for capturing exceptions and handling different exceptions accordingly.

III. Statement block formats1. try function: execution of program statements in the statement block generates an exception and then throws an exception. Try
{
// Program execution statement sequence}

2. throw function: throws an exception object when an exception occurs.
Throw (exception type expression );

3. catch function: the statement block is used to shop for matching the exception object thrown by throw and then process it.
Catch (exception type)
{
// Sequence of exception handling statements
}

Iv. Execution Process of Exception Handling1. Execute the Program Statement sequence in the try block;
2. If throw () is not executed during execution, the catch statement block in the exception handling area is skipped and the program runs down;
3. If an exception occurs during execution, the throw () Statement will throw an exception and enter the Exception Processing Area. The exception type expressions (objects) thrown by throw () will be followed by catch () type match in. The catch clause that gets the matching will capture and handle the exception and continue executing the statement in the exception handling area;
4. If a matched exception handling clause is not found (the exception is not captured), the program automatically calls the end function terminate () (the function terminate notifies the user and the system terminates the program ). Even if you cannot handle an exception, you should write code to capture the exception and output an appropriate error message before exiting.
Throwing an exception in a function keeps the program well structured. The exceptions thrown by the inner layer of the nested function are passed layer by layer from the inner layer. The passed exception is caught, and the function starts the rollback process.


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.