Categories: Java

try-catch block in Java

Java has two keywords for managing exceptions: try-catch block.

Java try block :

To contain code that might throw an exception in Java, use a try block. It has to be applied to the technique.

The try block’s remaining statements won’t run if an exception arises at that specific statement. Therefore, it is advised against keeping code in a try block that won’t throw an exception.

The catch or finally block in Java must come after the try block.

Syntax for try-catch block:
try{    
//code that may throw an exception    
}catch(Exception_class_Name ref){}    
  
Syntax for try-finally block:
try{    
//code that may throw an exception    
}finally{}    
Java catch block:

Java catch block is used to manage the Exception by defining the type of exception within the parameter. The parent class exception (i.e., Exception) or the generated exception type must be the declared exception. Declaring the created type of exception is, nevertheless, the best course of action.

Example without the usage of try-catch block:
public class TryCatchExample1 {  
  
    public static void main(String[] args) {  
          
        int data=100/0; //may throw exception   
          
        System.out.println("rest of the code");  
          
    }  
      
}  
Output:
Exception in thread "main" java.lang.ArithmeticException: / by zero
 at TryCatchExample1.main(TryCatchExample1.java:5)
Example with the usage of try-catch block:
public class TryCatchExample2 {  
  
    public static void main(String[] args) {  
        try  
        {  
        int data=50/0; //may throw exception   
        }  
            //handling the exception  
        catch(ArithmeticException e)  
        {  
            System.out.println(e);  
        }  
        System.out.println("rest of the code");  
    }  
      
}  
Output:
java.lang.ArithmeticException: / by zerorest of the code
Example : using try-catch-finally block:
public class TryCatchExample2 {  
  
    public static void main(String[] args) {  
        try  
        {  
        int data=50/0; //may throw exception   
        }  
            //handling the exception  
        catch(ArithmeticException e)  
        {  
            System.out.println(e);  
        }  
       
     
        finally
        { System.out.println("rest of the code");  
            System.out.println("finally block executed");
        }
         
        // rest program will be executed
        System.out.println("Outside try-catch-finally clause");
}   
}
Output:
java.lang.ArithmeticException: / by zero
rest of the code
finally block executed
Outside try-catch-finally clause

Note: also read about the Exception Handling in Java

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

2 days ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

3 weeks ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

1 month ago

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

1 month ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

2 months ago

Longest Substring with K Distinct Characters

Given an integer k and a string s, write a function to determine the length…

2 months ago