-1

I am fairly new to Java and unable to understand the flow of control in try-catch-finally blocks. Whenever a exception is caught in a catch block, the code after the catch block is also executed, whether I place it in a finally block or not. Then what is the use of finally block?

class Excp
{
 public static void main(String args[])
 {
  int a,b,c;
  try
  {
   a=0;
   b=10;
   c=b/a;
   System.out.println("This line will not be executed");
  }
  catch(ArithmeticException e)
  {
   System.out.println("Divided by zero"); 
  }
  System.out.println("After exception is handled");
 }
}

There is no difference if I place the last print statement inside the finally block.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Gaurav Bipul
  • 93
  • 1
  • 1
  • 4
  • Throw a `new RuntimeException()` inside the `try` and you'll notice that the code after the catch block isn't executed. Then add a finally block. – Kayaman Aug 24 '16 at 09:51
  • You use the `finally` block to set your program to a status it can work with after any exception happened. – CloudPotato Aug 24 '16 at 09:51
  • 1
    The code inside the finally block **always** executes, even if in the `try` or `catch` there is a `return` or an unhandled exception. This is explained [here](https://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html) which was found with a really easy google search. **Please use google.** – Ray O'Kalahjan Aug 24 '16 at 09:51

5 Answers5

2

It makes a difference if another exception (unhandled by your code) happens in the try/catch block.

Without finally, the last line wouldn't be executed. With finally, the code is executed no matter what.

This is particularly useful to perform cleanup tasks beyond the reach of the garbage collector: system ressource, database lock, file deletion etc...

Gherbi Hicham
  • 2,416
  • 4
  • 26
  • 41
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
1

Consider this:

try {
    a=0;
    b=10;
    c=b/a;
    System.out.println("This line will not be executed");
}
catch(ArithmeticException e){
    throw new RuntimeException("Stuff went wrong", e); 
}
System.out.println("This line will also not be executed");
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
1

the finally block can be used to be executed, even after a function returns.

e.g.

public boolean doSmth() {
  try {
   return true;
  }
  finally {
    return false;
  }
}

would return false;

Melv_80
  • 222
  • 1
  • 5
0

The finally block will also execute if an exception is thrown from the catch block, or if a different exception is thrown from the try block.

examples:

try {
  int a=5;
  int b=0;
  int c=a/b;
catch (NullPointerException e) {
  // won't reach here
} finally {
  // will reach here
}

You may also omit the catch block entirely and still guarantee that the finally block will be executed:

try {
  int a=5;
  int b=0;
  int c=a/b;
} finally {
  // will reach here
}
Shloim
  • 5,281
  • 21
  • 36
0

The code inside the finally block always executes, even if in the try or catch there is a return or an unhandled exception. This is explained here, which was found with a really easy google search. Please use google.