finally

Assigning null to variable in finally block [duplicate]

ぐ巨炮叔叔 提交于 2019-12-20 07:52:45
问题 This question already has answers here : Does a finally block always get executed in Java? (47 answers) Closed 5 years ago . The output of the following piece of code is "Test Passed"; can someone explain to me why ? public class Test { public static void main(String args[]) { System.out.println(new Test().print()); } protected StringBuilder print() { StringBuilder builder = new StringBuilder(); try { builder.append("Test "); return builder.append("Passed!!!"); } finally { builder = null; } }

(java) variable scope inside try { } when accessing from finally { }?

ⅰ亾dé卋堺 提交于 2019-12-20 05:23:22
问题 I noticed that when the following variables when in try { }, I couldn't use methods on them from finally for example: import java.io.*; public class Main { public static void main()throws FileNotFoundException { Try{ File src = new File("src.txt"); File des = new File("des.txt"); /*code*/ } finally{ try{ /*closing code*/ System.out.print("After closing files:Size of src.txt:"+src.length()+" Bytes\t"); System.out.println("Size of des.txt:"+des.length()+" Bytes"); } catch (IOException io){

(java) variable scope inside try { } when accessing from finally { }?

久未见 提交于 2019-12-20 05:21:26
问题 I noticed that when the following variables when in try { }, I couldn't use methods on them from finally for example: import java.io.*; public class Main { public static void main()throws FileNotFoundException { Try{ File src = new File("src.txt"); File des = new File("des.txt"); /*code*/ } finally{ try{ /*closing code*/ System.out.print("After closing files:Size of src.txt:"+src.length()+" Bytes\t"); System.out.println("Size of des.txt:"+des.length()+" Bytes"); } catch (IOException io){

Javascript error handling with try .. catch .. finally

十年热恋 提交于 2019-12-18 10:14:47
问题 I have a suspicion that I'm using the finally block incorrectly, and that I don't understand the fundamentals of its purpose... function myFunc() { try { if (true) { throw "An error"; } } catch (e) { alert (e); return false; } finally { return true; } } This function will run the catch block, alert "An error", but then return true. Why doesn't it return false? 回答1: The finally block contains statements to execute after the try and catch blocks execute but before the statements following the

Java's strange behavior while returning from finally block

℡╲_俬逩灬. 提交于 2019-12-17 19:38:19
问题 Try this piece of code. Why does getValueB() return 1 instead of 2? After all, the increment() function is getting called twice. public class ReturningFromFinally { public static int getValueA() // This returns 2 as expected { try { return 1; } finally { return 2; } } public static int getValueB() // I expect this to return 2, but it returns 1 { try { return increment(); } finally { increment(); } } static int counter = 0; static int increment() { counter ++; return counter; } public static

Is there such case when in try\finally block the finally won't be executed?

不问归期 提交于 2019-12-17 16:56:06
问题 I'm studying for my test in Object Oriented Programming and I was wondering if there is any case what so ever that considering the following code: try { do something } catch (someException e) { } finally { do something } the finally block will not execute? 回答1: Yes. If you crash the Java VM or otherwise muck things up via native code, cause the program to terminate, or loop/wait infinitely inside the try block. Those are the only three cases which will avoid executing the finally block. 回答2:

java try finally block to close stream

拈花ヽ惹草 提交于 2019-12-17 16:01:53
问题 I want to close my stream in the finally block, but it throws an IOException so it seems like I have to nest another try block in my finally block in order to close the stream. Is that the right way to do it? It seems a bit clunky. Here's the code: public void read() { try { r = new BufferedReader(new InputStreamReader(address.openStream())); String inLine; while ((inLine = r.readLine()) != null) { System.out.println(inLine); } } catch (IOException readException) { readException

Does a finally block always run?

大兔子大兔子 提交于 2019-12-17 01:39:20
问题 Is there any condition where finally might not run in java? Thanks. 回答1: from the Sun Tutorials Note: If the JVM exits while the try or catch code is being executed, then the finally block may not execute. Likewise, if the thread executing the try or catch code is interrupted or killed, the finally block may not execute even though the application as a whole continues. I don't know of any other ways the finally block wouldn't execute... 回答2: System.exit shuts down the Virtual Machine.

Multiple returns: Which one sets the final return value?

我与影子孤独终老i 提交于 2019-12-16 20:14:13
问题 Given this code: String test() { try { return "1"; } finally { return "2"; } } Do the language specifications define the return value of a call to test() ? In other words: Is it always the same in every JVM? In the Sun JVM the return value is 2 , but I want to be sure, that this is not VM-dependant. 回答1: Yes, the language spec defines that "2" is the result. If a VM does it differently, it's not spec-compliant. Most compilers will complain about it. Eclipse, for example, will claim that the

The semantic of call/cc and “ensure” in Ruby

醉酒当歌 提交于 2019-12-13 15:41:22
问题 As I know so far, Ruby is the only mainstream language that supports both call/cc and try/catch/finally (written as begin/rescue/ensure/end block). I am not familiar with Ruby, but my intuitive tell me that there are potential conflicts of that two, since call/cc allows arbitrarily control flow and ensure require some guaranteed control flow (some code path MUST be executed in a pre-defined situation, namely leaving the containing block). So, are there any conflicts exists in the language? if