AppDividend
Latest Code Tutorials

Finally block in Java Example | Java Finally Block Tutorial

0

Finally block in Java Example | Java Finally Block Tutorial is today’s topic. Finally is a keyword in java. It is a block of code in association with try/catch block which will execute the program even if it throws any Exceptions, which also means if there is an error like Runtime Error or Arithmetic Error in the program, it will show the statements which will come under finally block. It always executes after the try/catch block. We have already covered Exception Handling and Throw exception in Java.

Finally block in Java

It is a block that is used to execute necessary code such as closing connection, stream, etc. Java finally block always executed whether an exception is handled or not. The finally block follows, try or catch block.  The finally block must be associated with the try block, you cannot use finally without the try block. You should place those statements in this block that must be executed always.

See the following syntax.

try {
  //statements
  }
  Catch {
  //exception handling
  }
  finally {
  //statements 
  }

See the following program.

class program1 {
  public static void main(String args[]) {
    try {
      int div;
      div = 11 / 0; // error
    } catch (ArithmeticException er) {
      System.out.println("Number can't be divided by 0(zero)");
    } finally {
      System.out.println("finally block");
    }
  }
}

See the output.

Finally block in Java

 

#Check Different Cases

#Case 1:

Use of finally block where exception does not occur.

See the following program.

class program2 {
  public static void main(String args[]) {
    try {
      int num;
      num = 77 / 11;
      System.out.println("The output is =" + num);
    } finally {
      System.out.println("finally executed");
    }
    System.out.println("outside of finally block");
  }
}

See the output.

 

Java Finally Block Tutorial

#Case: 2

Use of finally block where exception occurs, but it is not handled. See the following program.

class program3 {
  public static void main(String args[]) {
    try {
      int num;
      num = 30 / 0;
      System.out.println("output is =" + num);
    } catch (NullPointerException er) {
      System.out.println(er);
    } finally {
      System.out.println("this is final block");
    }
    System.out.println("outside of finally block");
  }
}

See the following output.

 

Finally block in Java Example

#Case: 3

Use of finally block where exception occurs, and that is also handled.

See the following program.

class program4 {
  public static void main(String args[]) {
    try {
      int num;
      num = 30 / 0;
      System.out.println("output is =" + num);
    } catch (ArithmeticException er) {
      System.out.println(er);
    } finally {
      System.out.println("This is finally block");
    }
    System.out.println("outside of finally block");
  }
}

See the following output.

 

Finally in Java

#Cases where finally block doesn’t execute

  1. When we use the System.exit( )method.
  2. When the exception occurs in finally block only.
  3. When there is a death of thread.

Note 1:

For one try block, there can be zero or more catch block.

Note 2:

You can write only one finally block in a try/catch block.

At last, Finally block in Java Example is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.