finally

finally doesn't seem to execute in C# console application while using F5

点点圈 提交于 2019-12-08 15:51:55
问题 int i=0; try{ int j = 10/i; } catch(IOException e){} finally{ Console.WriteLine("In finally"); Console.ReadLine(); } The finally block does not seem to execute when pressing F5 in VS2008. I am using this code in Console Application. 回答1: The Visual Studio debugger halts execution when you get an uncaught exception (in this case a divide by zero exception). In debug mode Visual Studio prefers to break execution and give you a popup box at the source of the error rather than letting the

loss exception in block catch

廉价感情. 提交于 2019-12-07 02:35:24
问题 I run this code: public class User { public static void main(String args[]) { int array[] = new int[10]; int i = 1; try { System.out.println("try: " + i++); System.out.println(array[10]); System.out.println("try"); } catch (Exception e) { System.out.println("catch: " + i++); System.out.println(array[10]); System.out.println("catch"); } finally { System.out.println("finally: " + i++); Object o = null; o.hashCode(); System.out.println("finally"); } } } Result: try: 1 catch: 2 finally: 3

Where the finally is necessary?

限于喜欢 提交于 2019-12-06 20:05:59
问题 I know how to use try-catch-finally. However I do not get the advance of using finally as I always can place the code after the try-catch block. Is there any clear example? 回答1: Update : This is actually not a great answer. On the other hand, maybe it is a good answer because it illustrates a perfect example of finally succeeding where a developer (i.e., me) might fail to ensure cleanup properly. In the below code, consider the scenario where an exception other than SpecificException is

JAVA final finally finalize区别

风格不统一 提交于 2019-12-06 20:01:19
简单区别: 中等区别: 虽然这三个单词在 Java 中都存在,但是并 没有太多关联 : final : java 中的关键字,修饰符。 1. 如果一个类被声明为 final ,就意味着它 不能再派生出新的子类 ,不能作为父类被继承。因此,一个类不能同时被声明为 absrtact 抽象类的和 final 的类。 2. 如果将变量或者方法声明为 final ,可以保证它们在使用中不被改变 . 2.1 被声明为 final 的 变量 必须在 声明时给定初值 ,而在以后的引用中 只能读取,不可修改 。 2.2 被声明 final 的 方法只能使用 , 不能重载 。 finally : java 的一种异常处理机制。 finally 是对 Java 异常处理模型的最佳补充。 finally 结构使代码总会执行,而不管有无异常发生。使用 finally 可以维护对象的内部状态,并可以清理非内存资源。特别是在 关闭数据库连接 这方面,如果程序员 把数据库连接的 close() 方法放到 finally 中,就会大大降低程序出错的几率。 finalize : Java 中的一个方法名。 Java 技术使用 finalize() 方法在 垃圾收集器 将对象从内存中清除出去前,做必要的清理工作。这个方法是由垃圾收集器在确定这个对象没有被引用时对这个对象调用的。它是 在 Object 类中定义 的

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

关于异常一

倾然丶 夕夏残阳落幕 提交于 2019-12-06 10:54:25
我们经常会使用 try/catch/finally 语句块。当然, return 关键字使用也是很平常的事,但是不知道大家有没有注意个这样一个问题。当在 try 语句块里面使用 return 语句,在 finally 里面去修改 return 所要返回的内容会出现什么情况。首先,我们知道 return 是结束方法的标志,一旦方法执行到 return 语句就将返回不再往下执行。其次,我们也知道, finally 里面的语句是无论方法怎样执行,最后都要执行 finally 里面的语句。那么究竟是先执行 return 还是 finally 呢?下面通过两个小实验来解决这个问题。 首先看第一个例子 public class TestTryCatch { public static void main(String[] args){ TestTryCatch test = new TestTryCatch(); System.out.println(test.fun()); } public int fun(){ int i = 10; try{ //doing something return i; }catch(Exception e){ return i; }finally{ i = 20; } } } 输出结果: 10 OK ,很简单的一个例子,创建了一个方法 fun ,在方法里使用

'Finally' equivalent for If/Elif statements in Python

吃可爱长大的小学妹 提交于 2019-12-05 09:32:15
问题 Does Python have a finally equivalent for its if/else statements, similar to its try/except/finally statements? Something that would allow us to simplify this: if condition1: do stuff clean up elif condition2: do stuff clean up elif condition3: do stuff clean up ... ... to this: if condition1: do stuff elif condition2: do stuff elif condition3: do stuff ... ... finally: clean up Where finally would only be called only after a condition was met and its 'do stuff' run? Conversely, if no

Close connection and statement finally

与世无争的帅哥 提交于 2019-12-05 04:33:14
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(); } } 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 will throw NullPointerException . Also, if you are already on Java 7 , you should consider using try-with

Status of JSR/RET in JVM spec

为君一笑 提交于 2019-12-05 02:39:30
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 for the Java programming language that generates class files with version number 50.0 or below may use

Where the finally is necessary?

冷暖自知 提交于 2019-12-05 01:24:16
I know how to use try-catch-finally. However I do not get the advance of using finally as I always can place the code after the try-catch block. Is there any clear example? Update : This is actually not a great answer. On the other hand, maybe it is a good answer because it illustrates a perfect example of finally succeeding where a developer (i.e., me) might fail to ensure cleanup properly. In the below code, consider the scenario where an exception other than SpecificException is thrown. Then the first example will still perform cleanup, while the second will not, even though the developer