try-catch-finally

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

丶灬走出姿态 提交于 2019-11-27 07:05:48
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? Under what specific conditions will the code execute or not? EDIT: As NullUserException pointed out, the

how to use finally

早过忘川 提交于 2019-11-27 06:58:04
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 They differ if the try -block completes by throwing a java.lang.Throwable that is not a java.lang.Exception , for instance because it is a java.lang.Error such as AssertionError or OutOfMemoryError . the try-block completes abruptly using a

Why use finally instead of code after catch [duplicate]

六月ゝ 毕业季﹏ 提交于 2019-11-27 06:28:42
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(); 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. Look at your catch block - it's going to throw DAOException . So the statements after your catch block aren't going to be

If I return out of a try/finally block in C# does the code in the finally always run?

*爱你&永不变心* 提交于 2019-11-27 01:36:21
问题 It seems like it does as per some initial testing, but what I'd like to know is if it is guaranteed to return or if in some cases it can not return? This is critical for my application but I haven't found a use-case yet where it wouldn't return. I'd like to get expertise on the subject. 回答1: Anything in a finally block will always be executed regardless of what happens inside the try or catch blocks. It doesn't matter if you return form the method or not. The only exception to this is if the

Java Try Catch Finally blocks without Catch

北城以北 提交于 2019-11-26 23:57:17
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? 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 exception is thrown or not. Peter Lawrey A small note on try / finally : The finally will always execute unless

Why is try {…} finally {…} good; try {…} catch{} bad?

梦想的初衷 提交于 2019-11-26 21:20:50
I have seen people say that it is bad form to use catch with no arguments, especially if that catch doesn't do anything: StreamReader reader=new StreamReader("myfile.txt"); try { int i = 5 / 0; } catch // No args, so it will catch any exception {} reader.Close(); However, this is considered good form: StreamReader reader=new StreamReader("myfile.txt"); try { int i = 5 / 0; } finally // Will execute despite any exception { reader.Close(); } As far as I can tell, the only difference between putting cleanup code in a finally block and putting cleanup code after the try..catch blocks is if you

Is a finally block without a catch block a java anti-pattern?

为君一笑 提交于 2019-11-26 20:18:36
问题 I just had a pretty painful troubleshooting experience in troubleshooting some code that looked like this: try { doSomeStuff() doMore() } finally { doSomeOtherStuff() } The problem was difficult to troubleshoot because doSomeStuff() threw an exception, which in turn caused doSomeOtherStuff() to also throw an exception. The second exception (thrown by the finally block) was thrown up to my code, but it did not have a handle on the first exception (thrown from doSomeStuff()), which was the real

Behaviour of return statement in catch and finally

孤街醉人 提交于 2019-11-26 19:41:27
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 statement in catch block ? It is overridden by the one in finally , because finally is executed after

Try-catch-finally and then again a try catch

半腔热情 提交于 2019-11-26 15:46:16
问题 I have often come across situations like :- try{ ... stmts ... } catch(Exception ex) { ... stmts ... } finally { connection.close // throws an exception } which still needs a try - catch block inside finally. What is the best practice to overcome this? 回答1: Write a SQLUtils class that contains static closeQuietly methods that catch and log such exceptions, then use as appropriate. You'll end up with something that reads like this: public class SQLUtils { private static Log log = LogFactory

Can we use “return” in finally block [duplicate]

旧巷老猫 提交于 2019-11-26 15:25:48
问题 This question already has an answer here: Returning from a finally block in Java 5 answers Multiple returns: Which one sets the final return value? 7 answers Can we use return statement in finally block. Can this cause any problem? 回答1: Returning from inside a finally block will cause exceptions to be lost. A return statement inside a finally block will cause any exception that might be thrown in the try or catch block to be discarded. According to the Java Language Specification: If