Why do we use finally blocks?

前端 未结 11 2217
甜味超标
甜味超标 2020-11-27 11:43

As far as I can tell, both of the following code snippets will serve the same purpose. Why have finally blocks at all?

Code A:

try { /*          


        
相关标签:
11条回答
  • 2020-11-27 12:26

    If catch block throws any exception then remaining code will not executed hence we have to write finaly block.

    0 讨论(0)
  • 2020-11-27 12:31

    finally block in java can be used to put "cleanup" code such as closing a file, closing connection etc.


    The finally block will not be executed if program exits(either by calling System.exit() or by causing a fatal error that causes the process to abort).

    0 讨论(0)
  • 2020-11-27 12:33

    There may be times when you want to execute a piece of code no matter what. Whether an exception is thrown or not. Then one uses finally.

    0 讨论(0)
  • 2020-11-27 12:34
    • What happens if an exception you're not handling gets thrown? (I hope you're not catching Throwable...)
    • What happens if you return from inside the try block?
    • What happens if the catch block throws an exception?

    A finally block makes sure that however you exit that block (modulo a few ways of aborting the whole process explicitly), it will get executed. That's important for deterministic cleanup of resources.

    0 讨论(0)
  • 2020-11-27 12:34

    Still scrolling down? Here you go!

    This question gave me tough time back a while.

    try
    {
     int a=1;
     int b=0;
     int c=a/b;
    }
    catch(Exception ex)
    {
     console.writeline(ex.Message);
    }
    finally
    {
     console.writeline("Finally block");
    }
    console.writeline("After finally");
    

    what would be printed in the above scenario? Yes guessed it right:

    • ex.Message--whatever it is (probably attempted division by zero)

    • Finally block

    • After finally

      try
      {
          int a=1;
          int b=0;
          int c=a/b;
      }
      catch(Exception ex)
      {
          throw(ex);
      }
      finally
      {
          console.writeline("Finally block");
      }
      console.writeline("After finally");
      

    What would this print? Nothing! It throws an error since the catch block raised an error.

    In a good programming structure, your exceptions would be funneled, in the sense that this code will be handled from another layer. To stimulate such a case i'll nested try this code.

    try
    {    
     try
        {
         int a=1;
         int b=0;
         int c=a/b;
        }
        catch(Exception ex)
        {
         throw(ex);
        }
        finally
        {
         console.writeline("Finally block")
        }
        console.writeline("After finally");
    }
    catch(Exception ex)
    {
     console.writeline(ex.Message);
    }
    

    In this case the output would be:

    • Finally block
    • ex.Message--whatever it is.

    It is clear that when you catch an exception and throw it again into other layers(Funneling), the code after throw does not get executed. It acts similar to just how a return inside a function works.

    You now know why not to close your resources on codes after the catch block.Place them in finally block.

    0 讨论(0)
提交回复
热议问题