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.


Search
Categories


Print Article
Send to a friend
Save as PDF