finally

Terminate workers

喜欢而已 提交于 2019-12-12 03:44:55
问题 I've got a function which starts a number of worker threads. Each worker thread is encapsulated by an object, and the destructor of that object will try to join the thread, i.e calls if (thrd_.joinable()) thrd_.join(); . But it is unknown a priori how much work each worker will have to perform. the managing function assigns work units to the threads using a mutex and a condition variable. If there is no more work to do, a certain flag is set while the mutex is being held, and then all threads

.NET Will stop debugging run the code in the finally block?

丶灬走出姿态 提交于 2019-12-10 16:52:50
问题 Well, I've read (and learned) that the finally block doesn't always execute its code (even apart from pulling the plug). FYI For more information, see try catch finally question However, what I haven't found: Does my finally block get executed when I stop the debugger? Thanks! 回答1: No. When you stop the debugger before reaching the finally-block, it won't be executed. 来源: https://stackoverflow.com/questions/11084050/net-will-stop-debugging-run-the-code-in-the-finally-block

java try-catch-finally recursion question

ぃ、小莉子 提交于 2019-12-10 15:50:54
问题 public class Foo { public static void main(String[] args) { foo(); } public static void foo() { try { System.out.println("try"); foo(); } catch (Throwable e) { System.out.println("catch"); foo(); } finally { System.out.println("finally"); foo(); } } } who can explain the output of this code ? 1.output on eclipse (endless) client mode: try try .... ... ... tryfinallyfinally tryfinallyfinally try try try tryfinallyfinally tryfinallyfinally try tryfinallyfinally tryfinallyfinally try .... .... 2

finally与return之间的关系

牧云@^-^@ 提交于 2019-12-10 09:54:29
定论 问:finally语句一定会执行吗? 答: 1.如果没有执行相应的try语句则不会执行。 2.在try语句中如果调用System.exit(0)方法则不会执行。 问:finally会在什么时候执行? 答:如果在try/catch语句中调用转移指令例如:return,break,continue,throw等。则会在转移指令前执行。 总结 finally与return之间的关系 如果在finally中含有return语句,那么try/catch语句的return还有作用吗? 先看一段代码: /** * Created by gavin on 15-9-2. */ public class FinallyTest { public static void main(String[] args){ System.out.println(test1()); //3 System.out.println(test2()); //3 System.out.println(test3()); //2 System.out.println(test4()); //2 } public static int test1() { int i = 1; try { i = 2; return i; }finally { i++; return i; } } public static int

java那些事(八)之深入理解java异常处理机制

冷暖自知 提交于 2019-12-10 09:54:10
1. 引子 try…catch…finally恐怕是大家再熟悉不过的语句了,而且感觉用起来也是很简单,逻辑上似乎也是很容易理解。不过,我亲自体验的“教训” 告诉我,这个东西可不是想象中的那么简单、听话。不信?那你看看下面的代码,“猜猜”它执行后的结果会是什么?不要往后看答案、也不许执行代码看真正答案 哦。如果你的答案是正确,那么这篇文章你就不用浪费时间看啦。 <span style= "background-color: rgb(255, 255, 255);" > package Test; public class TestException { public TestException() { } boolean testEx() throws Exception { boolean ret = true ; try { ret = testEx1(); } catch (Exception e) { System.out.println( "testEx, catch exception" ); ret = false ; throw e; } finally { System.out.println( "testEx, finally; return value=" + ret); return ret; } } boolean testEx1() throws

try catch finally 顺序执行

别说谁变了你拦得住时间么 提交于 2019-12-10 09:37:30
结论: 1、不管有木有出现异常,finally块中代码都会执行; 2、当try和catch中有return时,finally仍然会执行; 3、finally是在return后面的表达式运算后执行的(此时并没有返回运算后的值,而是先把要返回的值保存起来,管finally中的代码怎么样,返回的值都不会改变,任然是之前保存的值),所以函数返回值是在finally执行前确定的; 4、finally中最好不要包含return,否则程序会提前退出,返回值不是try或catch中保存的返回值。 举例: 情况1 :try{} catch(){}finally{} return; 显然程序按顺序执行。 情况2 :try{ return; }catch(){} finally{} return; 程序执行try块中return之前(包括return语句中的表达式运算)代码; 再执行finally块,最后执行try中return; finally块之后的语句return,因为程序在try中已经return所以不再执行。 情况3 :try{ } catch(){return;} finally{} return; 程序先执行try,如果遇到异常执行catch块, 有异常:则执行catch中return之前(包括return语句中的表达式运算)代码,再执行finally语句中全部代码,

try{}catch{}finally{}笔试题注意事项

只谈情不闲聊 提交于 2019-12-10 05:56:24
try{}catch{}finally{}笔试题注意事项 try{}catch{}finally{}三者写在一起则是一个整体,当try中设置返回值时,则需要在三项执行完毕后,再返回try中的返回值,而最终的返回值则不再执行 public class TestException { //处理异常放在方法内部 public String testException ( ) { try { System . out . println ( "try开始执行" ) ; String str = null ; //str.length();//空指针异常 System . out . println ( "try执行完毕" ) ; return "try中的返回值" ; } catch ( Exception e ) { //e.printStackTrace();//输出异常名字 System . out . println ( "捕获到了异常" ) ; } finally { System . out . println ( "finally代码块执行了" ) ; } return "最终的返回值" ; } public static void main ( String [ ] args ) { String result = new TestException ( ) .

Close connection and statement finally

强颜欢笑 提交于 2019-12-10 04:19:21
问题 Which is better for finally block: finally { try { con.close(); stat.close(); } catch (SQLException sqlee) { sqlee.printStackTrace(); } } Or: finally { try { if (con != null) { con.close(); } if (stat != null) { stat.close(); } } catch (SQLException sqlee) { sqlee.printStackTrace(); } } 回答1: Better way to use is the 2nd one, because if an exception is thrown while initializing con or stat , they won't be initialized, and might be left initialized to null . In that case, using the 1st code

Status of JSR/RET in JVM spec

时间秒杀一切 提交于 2019-12-10 02:50:04
问题 There are some parts of the JVM specification which suggest that the operations JSR (Jump SubRoutine), JSR_W (Jump SubRoutine Wide) and RET (RETurn from subroutine) may be used only up to class file version 50.0 (JDK 1.6): 3.13 Compiling Finally (This section assumes a compiler generates class files with version number 50.0 or below, so that the jsr instruction may be used. See also §4.10.2.5.) And later: 4.10.2.5. Exceptions and finally To implement the try - finally construct, a compiler

What is the difference between finally and no finally?

风流意气都作罢 提交于 2019-12-08 17:19:20
问题 What is the difference between try { // action A } catch(Exception e) { // action B } finally { // action C } and try { // action A } catch(Exception e) { // action B } // action C I have read that you can return from inside a catch block and still have the finally block execute. Are there any other differences? 回答1: Things that happen within the finally block are guaranteed to occur no matter what happens in the try-catch-block. If an exception happens that is not encapsulated by Exception