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.