Exception Handling: Try, Catch and Throw - Practical Electron Microscopy and Database - - An Online Book - |
|||||||||||||
Microanalysis | EM Book http://www.globalsino.com/EM/ | |||||||||||||
================================================================================= | |||||||||||||
Exception handling in C++ as well as DM is built upon three keywords: In general, program statements, which you want to monitor for exceptions, are
contained in a try block. If an exception (i.e., an error) occurs within the try block, then it
is thrown by using the "throw" keyword. The exception is caught, using the "catch" keyword, and processed. The code, which you want to monitor for exceptions, must have been executed from within
a try block. Exceptions
that can be thrown by the monitored code are caught by a catch statement, which
immediately follows the try statement in which the exception is thrown. The general
form of try and catch blocks is, The try block in Lines 1~4 must contain the portion of the program that you want to monitor for errors. This portion can be as short as a few statements within one function, or as all-encompassing as a try block that encloses the main( ) function code. If this way is used, the main( ) function code would, in effect, cause the entire program to be monitored. When an exception is thrown, it is caught by one of its corresponding catch statement, which processes the exception. As seen from Lines 5 to 21, there can be more than one catch statement associated with a try block. The type of the exception determines which catch statement is used, namely, if the data type specified by a catch statement matches that of the exception, that catch statement is then executed (and all others are bypassed). When an exception is caught, its "arg" will receive its value. Any type of data can also be caught, including classes which are created. The general form of the throw statement is, "throw" statement generates the exception specified by exception. If this exception is to be caught, throw must then be executed either from within a try block itself, or from any function directly or indirectly called from within the try block. Table 1103. Examples of exception handling applications in DM.
|
|||||||||||||
================================================================================= | |||||||||||||
|
|||||||||||||