Difference between try-finally and try-catch

前端 未结 11 547
栀梦
栀梦 2020-12-23 03:05

What\'s the difference between

try {
    fooBar();
} finally {
    barFoo();
}

and

try {
  fooBar();
} catch(Throwable thro         


        
相关标签:
11条回答
  • 2020-12-23 03:22

    These are not variations, they're fundamentally different things. finally is executed always, catch only when an exception occurs.

    0 讨论(0)
  • 2020-12-23 03:25

    Finally block is always executed. Catch block is executed only when an exception that matches the blocks parameter is catched.

    0 讨论(0)
  • 2020-12-23 03:25

    Even in the first form you could log it in the calling method. So there is no big advantage unless you want to do some special handling right there.

    0 讨论(0)
  • 2020-12-23 03:30
    try {
        statements;
    } catch (exceptionType1 e1) {      // one or multiple
        statements;                 
    } catch (exceptionType2 e2) {
        statements;
    }    
    ...
    } finally {                                 // one or none
        statements;
    }
    
    1. All try statements must include either one catch clause or a finally clause
    2. It can have a multiple catch clauses but only one finally clause
    3. During any execution, if any errors occurs, then the Control is transferred to the appropriate Catch block and executes the statements and Finally block is executed.

    No Matter what The Finally block is always executed, So in General, Finally block is used, when you have sessions, Database connections or Files or sockets are open, then the code for closing those connections will be placed. This is just to make sure in an application no memory leaks or Any other issues should not occur.

    0 讨论(0)
  • 2020-12-23 03:31

    Finally and catch blocks are quite different:

    • Within the catch block you can respond to the thrown exception. This block is executed only if there is an unhandled exception and the type matches the one or is subclass of the one specified in the catch block's parameter.
    • Finally will be always executed after try and catch blocks whether there is an exception raised or not.

    So

    try {
      //some code
    }
    catch (ExceptionA) {
      // Only gets executed if ExceptionA 
      // was thrown in try block
    }
    catch (ExceptionB) {
      // Only executed if ExceptionB was thrown in try 
      // and not handled by first catch block
    }
    

    differs from

    try {
      //some code
    }
    finally {
      // Gets executed whether or not 
      // an exception was thrown in try block
    }
    

    significantly.

    If you define a try block you have to define

    1. one finally block, or
    2. one or more catch blocks, or
    3. one or more catch blocks and one finally block

    So the following code would be valid too:

    try {
      //some code
    }
    catch (ExceptionA) {
      // Only gets executed if 
      // ExceptionA was thrown in try block
    }
    catch (ExceptionB) {
      // Only executed if ExceptionB was thrown in 
      // try and not handled by first catch block
    }
    //even more catch blocks
    finally {
      // Gets executed whether or not an 
      // exception was thrown in try block
    }
    
    0 讨论(0)
提交回复
热议问题