Try-catch block in Java

Try Catch in Java – Exception handling

Try block

A series of statements where an exception may occur are contained in the try block.

A try block is always followed by a catch block, which handles the exception that occurs in an associated try block.

Either a catch block, a finally block, or both must come after a try block.

Catch block

 

Syntax of try catch in java:


Example of Try-catch Block


Output:

World

 

 

 

Example of Multiple Catch Blocks:

 

Points to remember

 

Example of Multiple catch block:


Output:

Arithmetic Exception

Execution Done

Example of Multiple Catch BlocKs


Output:

Arithmetic Exception

Execution Done

Another Example of Multiple Catch Blocks


Output:

Arithmetic Exception

Execution Done

Example of Catch Block without maintaining Order


Output:

Compile-time error

Visited 1 times, 1 visit(s) today

Comments are closed.

Close