Java try-catch block

try block

In our program the code which may cause an exception is called risky code. We have to place risky code inside try-block and the corresponding handling code inside catch block.

Java try block must be followed by either catch or finally block or try could be followed by both catch and finally blocks.

catch block

Java catch block is used to handle the exception by declaring the type of exception within the parameter. The declared exception must be the parent class exception ( i.e., Exception) or the generated exception type. However, the good approach is to declare the generated type of exception.

We cannot write any other code between try and catch block. So try-block should immediately followed by catch block.

We can write multiple catch blocks for a single try block.

Example 01 : Syntax of Java try-catch

Below are the examples and use cases with try-catch block

Example 02 : Code without exception handling

Output :

main begin
Exception in thread “main” java.lang.ArithmeticException: / by zero
at com.vidvaan.corejava.exception05.trycatchblock.Example02NoTryCatch.main(Example02NoTryCatch.java:7)

In the above example, Line number 7 and 8 are related, But Line number 10 is no way related to 7 and 8. As there is invalid arithmetic operation on line 7, So Line 8 is not executed. That is acceptable. But here important thing is line 10 is not executed even it is no way related to line number 7 and 8. It happens because we didn’t handle the exception.

Example 03 : Code with exception handling

In the above example we put all the code in try block. So our code is ready to handle the exception and program will not abnormally terminated when exception raised. But the bad thing is when exception raised non-exception related code also not executed. So we need to put only related code in the try block. Let’s fix this in the next example.

Example 04 : Related task code in try block

Output :

main begin
main end

In the above program, exception raised in the try block then it is handled by catch block. Even exception raised, ‘main end’ printed because we handled the exception. But here we don’t know what exception raised and where it is raised. To know that we need to print the exception stack trace info as shown in the following example.

Example 05 : Print Exception details from the Exception object

Output :

main begin
java.lang.ArithmeticException: / by zero
at com.vidvaan.corejava.exception05.trycatchblock.Example05PrintExceptionDetails.main(Example05PrintExceptionDetails.java:9)
main end

This is perfect !!! We got exception on line 10, So line 11 is not executed, then control goes to catch block and then line 15 executed and prints the exception details. Line 17 executed successfully. So we are making sure that even exception raised the remaining code executes without abnormal termination.

But in catch block we are not handling the exception, we are just printing exception. Handling could be done as per business needs, there are so many ways we could handle this exception

Example 06 : Handling Exception in catch block by printing issue details

Output :

begin
We can’t divide with zero
main end

In the above program, We are printing reason for the issue, User may fix the issue by looking into this message.

Example 07 : Handling Exception in catch block with default value

Output :

main begin
We can’t divide with zero, so dividing with default value’1′
Result of division : 50
main end

In this example, if we get exception we could just divide with one(as a default divider). Like this we can handle the exception in many ways as per business needs.

Example 08: catch super class of raised exception class

Output :

main begin
We can’t divide with zero
main end

Here we are catching the exception with super class ‘Exception’. But it is advisable to catch specific exception instead of generalized exception.

Example 09 : catch a different exception class instead raised exception

Output :

main begin
Exception in thread “main” java.lang.ArithmeticException: / by zero
at com.vidvaan.corejava.exception05.trycatchblock.Example09CatchDifferentExceptionInsteadRaisedException.main(Example09CatchDifferentExceptionInsteadRaisedException.java:11)

Here we are catching the NullPointerException, But JVM raised ArithmeticException. But our catch block could not catch the ArithmeticException. So always we need to catch issue related exception.

Example9 : Multiple tasks in one try block

Output :

main begin
java.lang.ArithmeticException: / by zero
at com.vidvaan.corejava.exception05.trycatchblock.Example10MultipleTasksInOneTryBlock.main(Example10MultipleTasksInOneTryBlock.java:12)
main end

Here we have two tasks code in one try block. We should not do that, because if first tasks fails to execute then second task also will not get executed, even it is no way related to the first task. So always put different tasks in different try blocks as shown below.

Example 11: Multiple tasks in multiple try blocks

Output :

main begin
java.lang.ArithmeticException: / by zero
at com.vidvaan.corejava.exception05.trycatchblock.Example11MultipleTasksInMultipleTryBlocks.main(Example11MultipleTasksInMultipleTryBlocks.java:12)
2nd element of array: 3
main end

Here we are handling different tasks in different try blocks and handling them with their specific exceptions.

Scroll to Top