try and catch java block

In this post, we will learn how we can use the try and catch block in java. Let’s discuss the try catch exception with example.

Here is the table content of the article will we will cover this topic.
1. What is try and catch block?
2. The important thing about try-catch?
3. try block with multiple catches?

What is try and catch block?

The try block contains a set of statements that can occur an exception. Place the code in a try block that might throw an exception. If an exception occurs in try block for a statement, then the rest code will not be executed which placed in the same block. When an exception occurs in the try block it will be handled by the catch block. The catch block is used to handle the occurred exceptions. To use the catch block you must declare the type of exception within the parameter.

try and catch java

The important thing about try and catch java

1. The try block must be followed by either catch or finally. So, you can’t use the try block alone. If you are placing, try block in your program then you must use either catch or finally.
2. The catch block always follows the try block.
3. You can’t place anything between the try and catch block. The catch block will be immediately placed by the try block.

try 
{
   // code that might throw an exception
} 
catch (ExceptionName ex)
{
   // Catch block
}

Example: Let’s take an example to better explain of try and catch block. Suppose we have a class UseOfTryCatchBlock and we are performing some arithmetic’s operations. In this example, we have some code that might create an exception it’s our responsibility to place the code in the try block. In the catch block, we are declaring the exception type as a parameter.

public class UseOfTryCatchBlock
{  
  public static void main(String args[])
  {  
  	try
  	{   
		System.out.println("This code is inside in the try block");  
		int a = 2;
		int b = 0;
    	 	int c = a/b;  
		System.out.println("result of calculation = "+ c);  
  	 }
   	catch (ArithmeticException e)
  	{
                e.printStackTrace();
  	}  
  	 //rest code of the program   
  	 System.out.println("This code is outside from the try block");  
  }  
}  

Output: This code is inside in the try block java.lang.ArithmeticException: / by zero at UseOfTryCatchBlock.main(UseOfTryCatchBlock.java:10) This code is outside from the try block

Explanation of Example: In this example, ArithmeticException occurred because the compiler tries to divide a number by zero. That exception is handled by catch block and print the reason of exception. The rest of the code works fine because that code placed outside the try-catch block.

try block with multiple catches

A try block can be followed by multiple catch blocks. Each catch block contains different exception types in parameters. If you want to handle different types of exceptions and perform some different actions on the occurrence of an exception, then use try block with multiple catches.

try 
{
   // code that might throw an exception
} 
catch (ExceptionTypeA e) 
{
   // Perform some action for ExceptionTypeA
} 
catch (ExceptionTypeB e) 
{
   // Perform some action for ExceptionTypeB
}
.
.
.
catch (ExceptionTypeN e) 
{
   // Perform some action for ExceptionTypeN 
}

Example: Let’s take an example to explain try and multiple catch blocks.

public class UseOfTryAndMultipleCatchBlock
{  
  public static void main(String args[])
  {  
  	try
  	{   
		System.out.println("This code is inside in the try block");  
		int a = 2;
		int b = 0;
    	 	int c = a/b;  
		System.out.println("result of calculation = "+ c);  
  	 }
   	catch (ArithmeticException e)
  	{
       	e.printStackTrace();
		System.out.println("ArithmeticException are handled");  
  	}
	catch (NumberFormatException e)
  	{
       	e.printStackTrace();
		System.out.println("NumberFormatException are handled");  
  	}
	catch (Exception e)
  	{
       	e.printStackTrace();
		System.out.println("Exception are handled");  
  	}

  	 //rest code of the program   
  	 System.out.println("This code is outside from the try block");  
  }  
}  

Output: This code is inside in the try block java.lang.ArithmeticException: / by zero ArithmeticException are handled This code is outside from the try block at UseOfTryAndMultipleCatchBlock.main(UseOfTryAndMultipleCatchBlock.java:10)

Explanation of example: In this example, we have multiple catch blocks with a single try block. We used different exception types in the different catch blocks. When the compiler executes the code of try block it throws an exception that is caught by the catch block. Compiler executes only one block at a time. So, when we are trying to divide a number by zero the ArithmeticException occurs and only the ArithmeticException catch block will be executed.

public class MainClass 
{
    public static void main (String [] args) 
    {
    	try
    	{
    		int a = 2/0;
    	}
    	catch(Exception e)
    	{
    		System.out.println("Exception : Divide by Zero");
    	}
    	catch(ArithmeticException e)
    	{
    		System.out.println("ArithmeticException : Divide by Zero");
    	}
    	finally 
    	{
    		System.out.println("Finally");
    	}
    }
}

Output: Exception in thread “main” java.lang.Error: Unresolved compilation problem:
Unreachable catch block for ArithmeticException. It is already handled by the catch block for Exception at MainClass.main(MainClass.java:13)

In the above example, it throws an exception because the catch block of ArithmeticException is unreachable. The catch block with Exception already catching the exception because it is superclass of ArithmeticException.

Leave a Comment