Basics of C Exception Handling

Basics of C++ Exception Handling

C++ exception handling is based on the following constructs:

  • try{ } - which is the name of a block that encloses a function or statements to be tested.

  • throw - which is an operator that throws a value, variable or data type.

  • catch(signature){ } - which is the name of a construct that carries out an action for a particular argument (i.e. what is thrown).

The construct: catch(signature){ } looks like a function but is it? It looks like a function because it has a name, a pair of parenthesis, a signature and a body. But functions are supposed to have a return type and functions are not supposed to be defined within another function or other construct. Clearly a catch(){ } violates these general rules of functions. Therefore maybe this construct should be called a statement and not called a function. But wait a moment. Is this not the same general form as constructors and the destructors have? These constructs do not have a return type. An argument against calling them a function is that they are not called like a function is. However, they look like a function and in many ways they act like a function and some even call them functions. Whether the catch(){ } constructs are or are not a function is a matter of semantics and adds little in the understanding of how they are used. Therefore these notes will from time to time call the catch(){ } construct a statement and sometimes a function.

The abstract forms of the try block and the catch() function are the following:

image from book

 Try {    // try block to be tested. } catch(datatype1 arg1) {    // body of action for argument arg1 of data type: datatype1. } catch(datatype2 arg2) {    // body of actions for argument arg2 of data type: datatype2 } ..... catch(datatypen argn) {    // catch functional body with argument argn of data    // type: datatypen     } 

image from book

Notice that there can be any number of catch() statements associated with a particular try{ } block. The catch(){ } statements above almost looks like function overloading. Take particular note of the fact that each catch() has only one argument. In addition the argument may be a data type and a variable or just a data type.

The abstract form of a throw is:

image from book

 throw exception; 

image from book

where exception may be a constant, a variable, a data type, or a function call. (As you will note in examples below, it is also possible for a throw to have no exception .) If an object is thrown, then the object is called an exception object. The point at which a throw is executed is called the throw point. When an exception is thrown, the try{ } block is exited and the program does not (directly) return to the block with the throw point.

Upon exiting the try{ } block, the program searches the following listed catch() statements to find one that matches the argument of the catch() statement listed with the item thrown. If there is a match, the statements within the catch() body are carried out. Once the catch() is carried out, the program jumps to the next statement after the last catch() and continues to execute the lines that follow. If no appropriate catch() is found, the system calls the terminate handler which in turn calls the abort() function. The call of this terminate handler will result in an abnormal ending of the program. The goal of a properly written set of error handling statements is to prevent the program from having an abnormal termination.

For example: except1.cpp. Run the program and notice that it has as output the following statements:

image from book

 This is the start Got into the try block The program throw the number 34 The program will now end. 

image from book

Notice in the try block that the statement after the throw is never reached.

What happens if there is no catch() that has an argument which matches the value thrown? In this case you would get an abnormal program termination. See except1a.cpp. The output of this program will be:

image from book

 This is the start Got into the try block Abnormal program termination. 

image from book

 Note:  The exact statement that will appear when a program terminates abnormally will depend on the SDK used by the programmer.

Notice in the example above that if you change the argument of the catch from a char to an int in this example then the program works correctly.

When using exception handling and a catch() can not handle the exception, the program should be terminated using exit() which is an orderly means of terminating the program.

Intermediate Business Programming with C++
Intermediate Business Programming with C++
ISBN: 738453099
Year: 2007
Pages: 142 © 2008-2017.
If you may any questions please contact us: