java exception handling

Java Exception Handling with Example

what is exception handling in java?

Exception Handling in Java is a powerful mechanism for dealing with runtime errors while maintaining the application’s normal flow. We’ll learn about Java exceptions handling, their types, and how to handle them in this tutorial. if you want to learn more about java exceptions then refer to Java Exception with Example

Example:

The main benefit of exception handling is that it keeps the application’s normal flow. An exception usually interrupts the application’s normal flow, which is why we need to handle them. Consider the following scenario:

Line 1;
Line 2;
Line 3;
Line 4;
Line 5;// Exception occurs
Line 6;
Line 7;
Line 8;
Line 9;
Line 10;

If an exception occurs at Line 5 of a Java program, the rest of the code, i.e. Line 6 through 10, will not be executed. The rest of the Line, on the other hand, will be executed when we handle exceptions. That is why in Java, we use exception handling.

Here is a list of various approaches to handling exceptions in Java.

  1. try…catch block
  2. finally block
  3. throw and throws keyword

Java try…catch block

In Java, the try-catch block is used to handle exceptions. The syntax of the try…catch block is as follows:

try {
// code
}
catch(Exception e) {
// code
}

  1. Within the try block, we have placed the code that may cause an exception. Every try block is immediately followed by a catch block.
  2. The catch block is called when an exception occurs. The catch block cannot be used in the absence of the try block.

Example: Exception handling using try…catch

class Main {
  public static void main(String[] args) {

    try {

      // code that generate exception
      int demo = 5 / 0;
      System.out.println("Rest of code in try block");
    }
    
    catch (ArithmeticException e) {
      System.out.println("ArithmeticException: " + e.getMessage());
    }
  }
}

Output

ArithmeticException: / by zero

In this example, we’re attempting to divide a number by zero. This code throws an exception in this case.

  • We put the code 5 / 0 inside the try block to handle the exception. When an exception occurs, the remainder of the code within the try block is now skipped.
  • The catch block catches the exception and executes the statements contained within it.
  • The catch block is skipped if none of the statements in the try block throw an exception.

Java finally block

Whether or not there is an exception, the finally block is always executed in Java. Finally, the finally block is optional. And there can only be one finally block for each try block.

Finally block syntax is as follows:

try {
//code
}
catch (ExceptionType1 e1) {
// catch block
}
finally {
// finally block always executes
}

If an exception occurs, the finally block follows the try…catch block. Otherwise, it is carried out after the try block. There can only be one finally block for each try block.

Example: Java Exception Handling using finally block

class Main {
  public static void main(String[] args) {
    try {
      // code that generates exception
      int demo = 5 / 0;
    }

    catch (ArithmeticException e) {
      System.out.println("ArithmeticException: " + e.getMessage());
    }
    
    finally {
      System.out.println("This is the finally block");
    }
  }
}

Output

ArithmeticException: / by zero
This is the finally block

In the preceding example, we are dividing a number by zero within the try block. This code throws an ArithmeticException in this case.

exception handling in java ,n handling in java with examples, exception handling program in java, types of exception handling in java,

The catch block catches the exception. The finally block is then executed.

Java throw and throws keyword

The throw keyword in Java is used to throw a single exception explicitly. When we throw an exception, the program’s flow shifts from the try block to the catch block.

Example: Exception handling using Java throw

class Main {
  public static void demo() {

    // throw an exception
    throw new ArithmeticException("Trying to divide by 0");
  }

  public static void main(String[] args) {
    demo();
  }
}

Output

Exception in thread “main” java.lang.ArithmeticException: Trying to divide by 0
at Main.divideByZero(Main.java:5)
at Main.main(Main.java:9)

Using the throw keyword, we explicitly throw the ArithmeticException in the preceding example.

Similarly, the throws keyword is used to specify the type of exception that can occur within the method. It’s mentioned in the method declaration.

Java Exception Keywords

To handle an exception in Java, there are five keywords available. Each is described in the table below.

KeywordDescription
tryThe “try” keyword is used to specify a block where an exception code should be placed. This means we can’t just use the try block. Either catch or finally must come after the try block.
catchTo handle the exception, the “catch” block is used. It must be preceded by a try block, so we can’t just use catch block. Later on, the final block can be added.
finallyThe “finally” block is used to run the program’s required code. Whether or not an exception is handled, it is run.
throwTo throw an exception, use the “throw” keyword.
throwsExceptions are declared using the “throws” keyword. It specifies that an exception may occur in the method. There isn’t any exception thrown. It’s always used in conjunction with a method signature.

You may like:

Java enum with Example

Java enum Constructor with Example

Java Inheritance with Example | Types of inheritance

Java final Keyword with Example

Not Equal Example in Java

Java Polymorphism with Example

Singleton Class in Java with Implementation and Example

Java Nested and Inner Class with Example

Hope this article will guide you to recognize all about Java Exceptions handling with Example that you needed and still if you have any problem or queries regarding this, post them in the comments section and we will be glad to assist you.

Exception handling in java, interview questions about exception handling in java, examples of exception handling in java, exception handling programme in java, types of exception handling in java, how to handle exceptions in java, exception handling keywords in java

This Post Has One Comment

Leave a Reply