What c ++ exception must know (1)

Source: Internet
Author: User

Exception Handling: 1) Handling Mechanism: Try {if any function in the try clause throws logic_error or calls another code function to throw such exceptions, the catch statement will be executed immediately ;} catch (logic_error & e) {the keyword throw indicates that the function will exit immediately and will not return to the calling handler program. Instead, it will exit until the exception-handling statement is found ;} n if an exception exists, create an exception object through the throw operation and throw it. N if no exception occurs during Protection Segment execution, the catch clause following the try block will not be executed. The program continues to execute the statements following the last catch clause following the try block. N catch clauses are checked in the order they appear after the try block. The matched catch clause will capture and handle exceptions (or continue throwing exceptions ). N if the matching processor is not found, the terminate function is automatically called. The default function is to call the abort termination program. 2) Throw and capture value: exception capture mechanism: you do not need to capture all exceptions in the Try block. When an exception occurs, the exception handling mechanism searches for matched handlers from the inside out, and only the first processing program that meets the conditions is executed. When writing a catch statement, you should first match the derived class and then the base class. The thrown value can be of any type, basic type or object. However, there is a type conversion problem during capture, (int-> double, or char * à string) change the original intention. To solve this problem, it throws and captures the object: Example: # include <iostream >#include <stdexcept> usingnamespace std; classFutureValueError: public logic_error {public: FutureValueError (stringe): logic_error (e) {}}; doublefuture () {throw FutureValueError ("illegalfuture_value"); return 2.0;} voidread () {try {double d = future (); cout <"intre Ad () "<endl; // in this case, Future statements will never be executed} catch (bad_alloc & e) {cout <" catch bad_allocerror "<e. what () <endl ;}}voidprocess_record () {try {read ();} catch (logic_error & e) {cout <"catchlogic_error" <e. what () <endl ;}} intmain () {process_record (); system ("pause") ;}running result: Analysis: There is no catch statement because FutureVauleError is thrown; therefore, the catch statement in the read () statement does not meet the conditions in the catch statement that is thrown to the read () function block. Therefore, the catch statement in the read () statement continues to be thrown to process_error, find the corresponding processing mechanism. The statements after the function is thrown cannot be executed. The root class of the exception level in the standard library exception Handling ++ standard library is called exception, the interface defining the exception class in the header file <exception> of the library is as follows: namespacestd {class exception {public: exception () throw (); exception (const exception &) throw (); exception & operator = (const exception &) throw (); virtual ~ Exception () throw (); virtual const char * what () const throw ();};}

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.