Example of try block and exception handling and debugging technology in C + + _c language

Source: Internet
Author: User
Tags assert exception handling throw exception

This paper illustrates the exception handling and debugging techniques of C + + try block, which helps readers to review and deepen their understanding of try block.

First, Format:

Throws an exception throw exception type such as Throw Runtime_error ("Data must refer to same ISBN");

try{
program-statements
}catch (exception-specifier)
{
handler-statement;
} catch (Exception-specifier)
{
handler-statement
}

Second, here need to note:

1. Once the catch clause has been executed, the program process immediately continues executing the statement that follows the last catch clause .

2. variables declared in a try block, including variables declared in a catch clause, cannot be referenced outside of a try .

3.There are many types of exceptions in Exception-specifier, such as the Runtime_error type is one of the standard library exception classes, and note that each standard library exception class defines a member function named what , such as Runtime_ Error Err,err.what (); Returns a string.

4. when you try to nest a try, throw an exception, first search for the function that throws the exception

5. How do I automatically invoke terminate (in the exception header file) to terminate the execution of a program after throwing an exception without a try block to capture it?

Third, debugging technology:

1. Use preprocessing Debugging for example:

#ifndef ndebug
cerr<< "Starting main" <<endl;
#endif
$CC-dndebug main.c 

#define NDEBUG preprocessing commands can be provided at the beginning of main.c

2. There are also some very useful constant __file__ (the path of the current file such as E:\MAIN.C) __line__ (the current line where __line__ is) __time__ (current time) __date__ (current date)

3. Common debugging techniques are the use of Ndebug preprocessing variables and an ASSERT (defined in the Cassert header file) to Preprocess macro assert (expr); If the expr result is False,assert output information and terminates the program, the operation is not done.

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.