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

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago