try-finally

object reference set to null in finally block

荒凉一梦 提交于 2019-12-06 18:47:21
问题 public void testFinally(){ System.out.println(setOne().toString()); } protected StringBuilder setOne(){ StringBuilder builder=new StringBuilder(); try{ builder.append("Cool"); return builder.append("Return"); }finally{ builder=null; /* ;) */ } } why output is CoolReturn, not null? Regards, Mahendra Athneria 回答1: The expression is evaluated to a value in the return statement, and that's the value which will be returned. The finally block is executed after the expression evaluation part of the

Will the 'finally' block fire even after a Junit test throws an Assertion Error from with in 'try' block?

末鹿安然 提交于 2019-12-06 17:49:59
问题 Will the writer.close() method inside the finally { } block run on an Junit Assertion Error? Assume the following code: @Test public void testWriter() { try { writer.open(); final List<MyBean> myBeans = new ArrayList<ProfileBean>(); /** Add 2 beans to the myBeans List here. **/ final int beansWritten = writer.writeBeans(myBeans); // Say this assertion error below is triggered org.junit.Assert.assertEquals("Wrong number of beans written.", -1, profilesWritten); } finally { writer.close(); //

Workaround for python 2.4's yield not allowed in try block with finally clause

混江龙づ霸主 提交于 2019-12-05 11:53:57
I'm stuck on python2.4, so I can't use a finally clause with generators or yield . Is there any way to work around this? I can't find any mentions of how to work around this limitation in python 2.4, and I'm not a big fan of the workarounds I've thought of (mainly involving __del__ and trying to make sure it runs within a reasonable time) aren't very appealing. You can duplicate code to avoid the finally block: try: yield 42 finally: do_something() Becomes: try: yield 42 except: # bare except, catches *anything* do_something() raise # re-raise same exception do_something() (I've not tried this

Why is a finally block *sometimes* not executed on ThreadAbortException if it contains an await?

自作多情 提交于 2019-12-05 01:55:54
问题 UPDATE: I don't think this question is a duplicate of Can ThreadAbortException skip finally? because (1) I'm not creating another thread, so there's no possibility of a race condition, and (2) this behavior only occurs if the finally block contains an await , which that other question doesn't mention. Consider this console program: class Program { static void Main() { try { T().GetAwaiter().GetResult(); } catch (ThreadAbortException) { Thread.ResetAbort(); } catch { } } static async Task

object reference set to null in finally block

荒凉一梦 提交于 2019-12-05 00:29:07
public void testFinally(){ System.out.println(setOne().toString()); } protected StringBuilder setOne(){ StringBuilder builder=new StringBuilder(); try{ builder.append("Cool"); return builder.append("Return"); }finally{ builder=null; /* ;) */ } } why output is CoolReturn, not null? Regards, Mahendra Athneria The expression is evaluated to a value in the return statement, and that's the value which will be returned. The finally block is executed after the expression evaluation part of the return statement. Of course, the finally block could modify the contents of the object referred to by the

使用try-with-resources替代try finally释放资源

风流意气都作罢 提交于 2019-12-04 08:00:43
1、旧社会 Java里,对于文件操作IO流、数据库连接等开销非常昂贵的资源,用完之后必须及时通过close方法将其关闭,否则资源会一直处于打开状态,直至程序停止,增加系统负担。 关闭资源的常用方式就是在finally块里是释放,即调用close方法。比如,我们经常会写这样的代码: public static void main(String[] args) { BufferedReader br = null; try { String line; br = new BufferedReader(new FileReader("d:\\testing.txt")); while ((line = br.readLine()) != null) { System.out.println(line); } } catch (IOException e) { // handle exception } finally { try { if (br != null) { br.close(); } } catch (IOException ex) { // handle exception } } } 可以看出,为了关闭资源以及处理关闭资源时可能出现的异常,不得不写一大推代码。 2、新时代 2.1 使用新写法 从Java 7开始,jdk提供了一种更好的方式关闭资源,使用try-with

Why is a finally block *sometimes* not executed on ThreadAbortException if it contains an await?

こ雲淡風輕ζ 提交于 2019-12-03 16:26:41
UPDATE: I don't think this question is a duplicate of Can ThreadAbortException skip finally? because (1) I'm not creating another thread, so there's no possibility of a race condition, and (2) this behavior only occurs if the finally block contains an await , which that other question doesn't mention. Consider this console program: class Program { static void Main() { try { T().GetAwaiter().GetResult(); } catch (ThreadAbortException) { Thread.ResetAbort(); } catch { } } static async Task Abort() { //await Task.Delay(1); // A Thread.CurrentThread.Abort(); // B } static async Task T() { try {

Should I put a try-finally block after every Object.Create?

六月ゝ 毕业季﹏ 提交于 2019-12-03 09:51:45
问题 I have a general question about best practice in OO Delphi. Currently, I put try-finally blocks anywhere I create an object to free that object after usage (to avoid memory leaks). E.g.: aObject := TObject.Create; try aOBject.AProcedure(); ... finally aObject.Free; end; instead of: aObject := TObject.Create; aObject.AProcedure(); .. aObject.Free; Do you think it is good practice, or too much overhead? And what about the performance? 回答1: In my opinion, there is only one reason an objects

Why does the Java Compiler copy finally blocks?

旧街凉风 提交于 2019-12-03 08:33:27
问题 When compiling the following code with a simple try/finally block, the Java Compiler produces the output below (viewed in the ASM Bytecode Viewer): Code: try { System.out.println("Attempting to divide by zero..."); System.out.println(1 / 0); } finally { System.out.println("Finally..."); } Bytecode: TRYCATCHBLOCK L0 L1 L1 L0 LINENUMBER 10 L0 GETSTATIC java/lang/System.out : Ljava/io/PrintStream; LDC "Attempting to divide by zero..." INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/String;

Why does the Java Compiler copy finally blocks?

五迷三道 提交于 2019-12-02 22:19:00
When compiling the following code with a simple try/finally block, the Java Compiler produces the output below (viewed in the ASM Bytecode Viewer): Code: try { System.out.println("Attempting to divide by zero..."); System.out.println(1 / 0); } finally { System.out.println("Finally..."); } Bytecode: TRYCATCHBLOCK L0 L1 L1 L0 LINENUMBER 10 L0 GETSTATIC java/lang/System.out : Ljava/io/PrintStream; LDC "Attempting to divide by zero..." INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/String;)V L2 LINENUMBER 11 L2 GETSTATIC java/lang/System.out : Ljava/io/PrintStream; ICONST_1 ICONST_0 IDIV