Java – Exceptions

When the Java interpreter detects an error or exception while executing a programme, it normally stops and displays an error message. Error handling, also known as exception handling, is the process of dealing with such situations, and Java provides three keywords for it:

  • try: It is used to check for errors in a block of statements.
  • catch: It has a set of statements that run when an error occurs and manage the situation.
  • finally: It has a block of statements that executes regardless of the outcome of the try-catch blocks.

try-catch blocks

The variable z is undefined in the example below, resulting in an error. Because the error happened in the try section of the statement, it will be handled by the statement’s catch block. Without the try block, the programme would terminate when it encounters an error and display an error message.

Syntax

Java-S: try-with-resources
try{
  statements;
} catch (Exception e) {
  statements;
}


Example:

public class MyClass {
  public static void main(String[] args) {
    try{
      int x = 10, y = 0, z;
      z = x/y;
      System.out.println(z);
    } catch (Exception e) {
      System.out.println("An error occurred.");
    }
  }
}


The output of the above code will be:

An error occurred.

Many catch blocks

Multiple catch blocks can be used in a Java application to handle different types of exceptions.

Syntax

try{
  statements;
} catch (ExceptionType_1 e) {
  statements;
} catch (ExceptionType_2 e) {
  statements;
} 
...
...
} catch (Exception e) {
  statements;
}


Example:

The variable z is undefined in the example below, resulting in an ArithmeticException. As a result, when an error occurs, the catch block of code is run.

public class MyClass {
  public static void main(String[] args) {
    try{
      int x = 10, y = 0, z;
      z = x/y;
      System.out.println(z);
    } catch (NumberFormatException e) {
      System.out.println("Number Format error.");
    } catch (ArithmeticException e) {
      System.out.println("Arithmetic operation error.");
    } catch (Exception e) {
      System.out.println("An error occurred.");
    }
  }
}


The output of the above code will be:

Arithmetic operation error.

Finally block

In the example below, the finally block of the statement is run independently of the results of the try-catch block, allowing the computer to shut the file before continuing.

Syntax

try{
  statements;
} catch (Exception e) {
  statements;
} finally {
  statements;
}


Example:

The finally block of code is used in the example below, and it is run after the try-catch blocks of code.

public class MyClass {
  public static void main(String[] args) {
    try{
      int x = 10, y = 0, z;
      z = x/y;
      System.out.println(z);
    } catch (NumberFormatException e) {
      System.out.println("Number Format error.");
    } catch (ArithmeticException e) {
      System.out.println("Arithmetic operation error.");
    } catch (Exception e) {
      System.out.println("An error occurred.");
    } finally {
      System.out.println("try-catch block finished.");
    }
  }
}


The output of the above code will be:

Arithmetic operation error.
try-catch block finished.

 

Click here to open the online java compiler.

Also Read: How to Convert Timestamp to Date in Java

Leave a Reply

Your email address will not be published.