try-catch-finally

What are the circumstances under which a finally {} block will NOT execute?

*爱你&永不变心* 提交于 2019-11-26 13:02:39
问题 In a Java try{} ... catch{} ... finally{} block, code within the finally{} is generally considered \"guaranteed\" to run regardless of what occurs in the try/catch. However, I know of at least two circumstances under which it will not execute: If System.exit(0) is called; or, if an Exception is thrown all the way up to the JVM and the default behavior occurs (i.e., printStackTrace() and exit) Are there any other program behaviors that will prevent the code in a finally{} block from executing?

how to use finally

只谈情不闲聊 提交于 2019-11-26 12:58:55
问题 I never properly understood the use of the finally statement. Can anyone tell me what the difference is between: try { a; block; off; statements; } catch (Exception e) { handle; exception; e; } finally { do; some; cleanup; } on the one hand and: try { a; block; off; statements; } catch (Exception e) { handle; exception; e; } do; some; cleanup; On the other 回答1: They differ if the try -block completes by throwing a java.lang.Throwable that is not a java.lang.Exception , for instance because it

Why use finally instead of code after catch [duplicate]

China☆狼群 提交于 2019-11-26 11:59:40
问题 This question already has an answer here: Why do we use finally blocks? 10 answers Why do this } catch (SQLException sqle) { sqle.printStackTrace(); } finally { cs.close(); rs.close(); } Instead of this } catch (SQLException sqle) { sqle.printStackTrace(); } rs.close(); cs.close(); 回答1: Because if an exception gets thrown no code after the try block is executed unless the exception is caught. A finally block is always executed no matter what happens inside your try block. 回答2: Look at your

Is it bad practice to return from within a try catch finally block?

橙三吉。 提交于 2019-11-26 11:52:40
问题 So I came across some code this morning that looked like this: try { x = SomeThingDangerous(); return x; } catch (Exception ex) { throw new DangerousException(ex); } finally { CleanUpDangerousStuff(); } Now this code compiles fine and works as it should, but it just doesn\'t feel right to return from within a try block, especially if there\'s an associated finally. My main issue is what happens if the finally throws an exception of it\'s own? You\'ve got a returned variable but also an

Java Try Catch Finally blocks without Catch

拈花ヽ惹草 提交于 2019-11-26 09:16:22
问题 I\'m reviewing some new code. The program has a try and a finally block only. Since the catch block is excluded, how does the try block work if it encounters an exception or anything throwable? Does it just go directly to the finally block? 回答1: If any of the code in the try block can throw a checked exception, it has to appear in the throws clause of the method signature. If an unchecked exception is thrown, it's bubbled out of the method. The finally block is always executed, whether an

Behaviour of return statement in catch and finally

让人想犯罪 __ 提交于 2019-11-26 07:22:20
问题 Please see the following code and explain the output behavior. public class MyFinalTest { public int doMethod(){ try{ throw new Exception(); } catch(Exception ex){ return 5; } finally{ return 10; } } public static void main(String[] args) { MyFinalTest testEx = new MyFinalTest(); int rVal = testEx.doMethod(); System.out.println(\"The return Val : \"+rVal); } } The result is the return Val : 10. Eclipse shows a warning: finally block does not complete normally . What happens to the return

How does Java's System.exit() work with try/catch/finally blocks? [duplicate]

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 03:54:58
问题 This question already has answers here : Does a finally block always get executed in Java? (47 answers) Closed 3 years ago . I\'m aware of headaches that involve returning in try/catch/finally blocks - cases where the return in the finally is always the return for the method, even if a return in a try or catch block should be the one executed. However, does the same apply to System.exit()? For example, if I have a try block: try { //Code System.exit(0) } catch (Exception ex) { //Log the

Returning from a finally block in Java

限于喜欢 提交于 2019-11-25 23:30:07
问题 I was surprised recently to find that it\'s possible to have a return statement in a finally block in Java. It seems like lots of people think it\'s a bad thing to do as described in \'Don\'t return in a finally clause\'. Scratching a little deeper, I also found \'Java\'s return doesn\'t always\' which shows some pretty horrible examples of other types of flow control in finally blocks. So, my question is, can anyone give me an example where a return statement (or other flow control) in a

Does a finally block always get executed in Java?

纵饮孤独 提交于 2019-11-25 22:18:06
问题 Considering this code, can I be absolutely sure that the finally block always executes, no matter what something() is? try { something(); return success; } catch (Exception e) { return failure; } finally { System.out.println(\"I don\'t know if this will get printed out\"); } 回答1: Yes, finally will be called after the execution of the try or catch code blocks. The only times finally won't be called are: If you invoke System.exit() If you invoke Runtime.getRuntime().halt(exitStatus) If the JVM