Electron microscopy
 
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:
         i) try,
         ii) catch,
         iii) throw.

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,
           Exception Handling: Try, Catch and Throw

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

"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.

Script
Note
Exception Handling
Script file

◆ The output of the script is:

◆ The try block in Lines 11~15 has four statements. Within the try block, only three of the four statements will execute: the first two statements and the throw. Once an exception has been thrown, control passes to the catch expression, and the try block is then terminated. That is, Line 15 will not execute. The program execution is then transferred to catch.

◆ The catch statement that processes exceptions in Lines 20 and 21. After the catch statement executes, program control then continues with the statements following the catch, so that program execution can continue normally.

◆ In cases where the error cannot be fixed, a catch block then will usually end with a call to exit( ) or abort( ), or terminate program execution.

◆ Lines 15 and 24 do not execute since throw ("99") in Line 14 and thus catch () in Lines 18 to 22 execute.


Script file
◆ The output of the script is:

◆ The output of this script is the same as that of the script above since the "test" IF statement is true in Line 14.

Script file
◆ The output of the script is:

◆ The output of this script is different from that of the script above since the "test" IF statement is not true in Line 14, and thus throw ("99") and catch in Lines 18 to 22 do not execute. However, the Lines 15 and 24 executes.

Script file

◆ The output of this script is the same as that of the script above since the "test" IF statement in Line 18 is not true based on the input from Line 34.

◆ The output of the script is:


Script file

◆ There are two throw (Lines 18 and 19). One of them is true (See Lines 19 and 35).

◆ Therefore, the output is:

 

 

=================================================================================