Coupon Accepted Successfully!


The Keywords Try, Throw, And Catch

The exception-handling technique passes the control of a program from a location of exception in a program to an exception-handler routine linked with the try block. An exception-handler routine can only be called by the throw statement.

(a) try: The try keyword is followed by a series of statements enclosed in curly braces.

Syntax of try statement



statement 1;

statement 2;


(b) throw: The function of the throw statement is to send the exception found. The declaration of the throw statement is as follows:

Syntax of throw statement

throw (excep);

throw excep;

throw // re-throwing of an exception

The argument excep is allowed to be of any type, and it may be a constant. The catch block associated with the try block catches the exception thrown. The control is transferred from the try block to the catch block. The throw statement can be placed in a function or is a nested loop, but it should be in the try block. After throwing the exception, control passes to the catch statement.

(c) catch: Similar to the try block, the catch block also contains a series of statements enclosed in curly braces. It also contains an argument of an exception type in parentheses.

Syntax of catch statement



Statement 1;

Statement 2;


catch ( argument)


statement 3; // Action to be taken


When an exception is found, the catch block is executed. The catch statement contains an argument of exception type, and it is optional. When an argument is declared, the argument can be used in the catch block. After the execution of the catch block, the statements inside the blocks are executed. In case no exception is caught, the catch block is ignored, and if a mismatch is found, the program is terminated.

Test Your Skills Now!
Take a Quiz now
Reviewer Name