| Type: |
|
| Level: |
Expert
|
| Date: |
2006-Dec-12
|
| Visited: |
11309 times
|
| Rating: |

|
| Author: |
Alex Tutorialized |
|
|
This tutorial will explain Exception Handling.
Exception handling allows you to manage run-time errors in an orderly fashion. In the 'C' program, every call to the function must be examined by the program. If else statement should surround each function call, and statements must be inserted to handle errors. The source code becomes large and difficult to read. Also some function cannot return an Errorvalue.
Example
if(somefunc() == ERROR_RETURN_VALUE)
//handle the error or call error-handler function
else
//proceed normally
if(another func()==NULL)
//handle the error or call error handler function
else
//proceed normally
if(thirdfunc()==0)
//handle the error or call-handler function
else
//proceed normally
|
The three keywords throw, try and catch are used to handle exceptions. Function returning using the exception handling mechanism is referred to as throwing an exception. The keyword throw is used to throw an exception. Try keyword surrounds a block of statements which may generate exception. Catch block catches the exception thrown by throw and handles them appropriately. The general forms of try and catch are shown above.
Example
try{
// try block
}
catch (type 1 arg){
// catch block
}
catch (type2 arg){
// catch block
}
catch (type3 arg){
// catch block
}
|
The relationship between the three words try, catch and throw is shown in the figure. The try block code normally invokes a function that detects an exception. The function which is in the throw block returns the control to the catch block. There can be many catch blocks, each corresponding to a particular type of exception. The function throwing an exception indicates the specific type of exception which in turn decides the catch statements to be executed.
posted on 2009-Jun-11 | 11:48:01 PM