Is it valid to have finally block without try and catch?

泄露秘密 提交于 2020-08-21 05:07:11

问题


I am trying to use the finally block without using the try/catch blocks but getting the error in Eclipse.

Can I use the finally block without using the try/catch blocks?


回答1:


finally should have atleast a try block, catch is optional. The point of finally blocks is to make sure stuff gets cleaned up whether an exception is thrown or not. As per the JLS

A finally clause ensures that the finally block is executed after the try block and any catch block that might be executed, no matter how control leaves the try block or catch block.

Hence a finally should always be preceded by a try block.




回答2:


You must have a try block with a finally block. The try block defines which lines of code will be followed by the finally code. If an exception is thrown prior to the try block, the finally code will not execute.

Adding catch blocks is optional:

try {

  // something

} finally {
  // guaranteed to run if execution enters the try block
}



回答3:


try {
    // Block of code with multiple exit points
}
finally {
    // Block of code that must always be executed when the try block
    // is exited, no matter how the try block is exited
}



回答4:


If an exception is thrown prior to the try block, the finally code will not execute. The finally block always executes when the try block exits. So you can use finally without catch but you must use try.




回答5:


The finally block always executes when the try block exits. So you can use finally without catch but you must use try.

For more details check doc here




回答6:


The reason why you cannot have a finally without a try is because you could have multiple finally statements in the same scope and the try indicates what block of code the finally pertains to in case an error occurs.

Another interesting feature of the finally is that it must execute no matter what when the try is entered. For example what if you use a goto to skip over your finally statement? If the goto is inside of the try it will execute the finally statement however if the goto is above/outside the try statement then it will skip the finally code. finally is relevant only to the code that is surrounded in the try. If you have no try then the finally is not relevant to anything.




回答7:


From Oracle Trails:

The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs.

From the above statement, you cannot have finally block alone on its own. below are the combinations allowed.

  1. try catch finally

  2. try catch

  3. try finally




回答8:


The code within the finally block is guaranteed to execute if program control flow enters the corresponding try block. So it makes no sense to have a finally without a try.

The only exception to this is if the program encounters a System.exit(...) call before the finally block, as that shuts down the virtual machine.




回答9:


No you can't

you can use Try-catch-finally or try-finally

 try {

    }catch (Exception e){

    }
    finally{

    }

or

try {

    }
    finally{

    }



回答10:


  • Using only try block is not correct.
  • Try block can be used with only one block from catch or finally block.
  • You can use try block with catch and finally.If you use finally block with try block then catch block become optional.



回答11:


  • A try statement should have either catch block or finally block, it can have both blocks.

  • We can’t write any code between try-catch-finally block.

  • We can’t have catch or finally clause without a try statement.

  • We can have multiple catch blocks with a single try statement.try-catch blocks can be nested similar to if-else statements.

  • We can have only one finally block with a try-catch statement.



来源:https://stackoverflow.com/questions/17314724/is-it-valid-to-have-finally-block-without-try-and-catch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!