finally in exception handling

2019-02-23 02:52发布

What exactly does a finally block in exception handling perform?

8条回答
你好瞎i
2楼-- · 2019-02-23 03:32

It holds code that should always be executed, regardless of whether an exception occurs.

For example, if you have opened a file, you should close it in the finally block to ensure that it will always be closed; if you closed it in the try block, an earlier exception would cause execution to jump straight to the catch block and skip closing the file.

See the Java tutorials for more details.

查看更多
甜甜的少女心
3楼-- · 2019-02-23 03:32

finally keyword is used just to make sure that code present in finally block must execute in all circumstances irrespective of exception occurances.

for eg:

 try{
     }
     catch(Exception e){
     }
      finally{
             System.out.print("finally executed");
            }

Note: In above case finally will always execute.

查看更多
一纸荒年 Trace。
4楼-- · 2019-02-23 03:34

It executes no matter if you get into the catch block or not, meaning that is a great place for disposing of objects and doing other cleanups.

查看更多
淡お忘
5楼-- · 2019-02-23 03:36

The finally block always executes, regardless of whether or not the exception was thrown. The classic use example I can think of is closing files.

FileOutputStream stream = null;
try{
    // do stuff with the stream here
} catch (IOException ex){
    // handle exception
} finally{
    // always close the stream
    if(stream != null){
        stream.close();
    }
}
查看更多
我命由我不由天
6楼-- · 2019-02-23 03:37

If you return a value in your try or catch block as well as in the finally block, keep in mind that the finally block's return value is what you'll end up with (the last block executed). That means if you try some code that does NOT throw an Exception and is supposed to return a value, but your finally block is also supposed to return a value, the finally block's value is what will actually be returned. This SO thread talks about that very point. I don't believe returning a value inside a try or catch is usually necessary or the best idea. Also note that System.exit(0) kills the JVM and thus halts execution before anything else runs, which might render your finally block unexecuted.

查看更多
走好不送
7楼-- · 2019-02-23 03:39

Though there are many answers have already given that finally block is required to execute some piece of code in all the conditions whether there is some interruption due to exception, or some bad code, or you return the program control flow from try block, Here I'm adding an example to explain the need of finally block;

Let's suppose you have borrowed a pen from your friend. You use it and then return ( I consider you a gentleman). Now whatever happens, you have to return the pen. You can handle various situations and you put most unavoidable condition in finally block.

//Borrow the pen
try{
  //Use the pen
}catch(StolenPen how){
  //Buy new pen
}catch(InkFinished how){
  //Refill the pen
}catch(SomethingWrong how){
  //Buy new pen
}finally{
  //Return new pen
}
查看更多
登录 后发表回答