0

For the last few days I am trying to dig deep in java, I came across try catch finally exception handling.

I know finally executes no matters what, but I want to know "WHY" we can also handle the exception in catch or after catch, why finally?

I came up with an answer

https://stackoverflow.com/a/23537289/7794329

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

        double p = 1.0D;
        String str = "bla";
        try{
            p = Double.valueOf(str);
        }
        catch(Exception ex){
            System.out.println("Exception Happened");
            return;  //return statement here!!!
        }finally{
            System.out.println("Finally");
        }
        System.out.println("After finally");

        System.out.println("again finally");
    }
}

output

Exception Happened
Finally

what if i remove that return; and handle after the code of finally. It will run.. so why we need finally only because of return statement? is it like i am doing mistakes by returing the code and forcefully handling it in finally.I can also make a flag in catch and put it in a if else block to handle the exception in catch

Why do we use finally blocks?

I really completely didn't understand can someone let me understand in plain language? Why do we use finally if we can handle it from try catch or even catch fails we can handle it in if else like if catch got exception run this... else...

Community
  • 1
  • 1
Aman
  • 806
  • 2
  • 12
  • 38
  • It was used often to allow you to close resources, like files etc, in a (relatively) safe manner, although now days, with `Closeable` and `try-with-resource` it's less of an issue. Basically, you need to use `finally` when there is some action which MUST be carried out regardless of how the `try` exited – MadProgrammer Apr 03 '17 at 07:41
  • The `return`-statement causes the method to end after executing the `final`-block. Final blocks are used to guarantee certain things, i.e. imagine having a specific service crash (bank-transaction, whatever), what'd you want to end up with, a dangling transaction, or a clean space with a notification being forwarded to the user? :) So, basically, clean-up is one of the possibilities that final-blocks offer you. – Seth Apr 03 '17 at 07:41
  • @MadProgrammer agreed but we can also handle it in if/else block with a flag in try catch. why finally? – Aman Apr 03 '17 at 07:55
  • @doe Because the `finally`-block is (*almost*) always executed (*except for your server exploding & things like that*), whereas the `else`-block of an `if-else` may very well not be executed at all due to a critical failure resulting in the program shutting down. – Seth Apr 03 '17 at 07:57
  • @Seth i am little confused about critical failures what would be they? they can also effect finally block or not?....what i am saying if there is any error occur i wil use a flag in try and catch both and according to that i will handle the both in if else block. so i guess there will be no use of finally.. if i'm correct – Aman Apr 03 '17 at 08:02
  • @doe Take a runtime exception as an example. Your program will forcefully shutdown, abandoning everything, *except for the `finally`-block.* Your flag-business works without "hard" failures, but not with those. I'd suggest reading some articles about this topic :) – Seth Apr 03 '17 at 08:05
  • @Seth thanks, yup i m on it trying to make it clear thanks for you answer :) – Aman Apr 03 '17 at 08:07
  • @doe Because it does it in a well document and guaranteed manner, which is simple to implement. Do you really want to set up more flags and states in your code just to determine if the `catch` block was caught? What if you had a number of `catch` blocks? What if you had states in the `try` which `return`'ed? – MadProgrammer Apr 03 '17 at 08:27

1 Answers1

1

If an exception is thrown, the finally block runs after the catch block and then the control flow goes to wherever the catch block decided, which was the return. If you get rid of the return statement, you'll find those other things print.

muzzlator
  • 742
  • 4
  • 10
  • what if i remove that return; and handle after the code of finally. It will run.. so why we need finally only because of return statement? is it like i am doing mistakes and forcefully handle it in finally. – Aman Apr 03 '17 at 07:48