我们可以在finally块中使用return语句.这会导致任何问题吗?
我从来没有正确理解finally语句的用法.谁能告诉我两者之间的区别是什么:
try {
a;
block;
off;
statements;
} catch (Exception e) {
handle;
exception;
e;
} finally {
do;
some;
cleanup;
}
Run Code Online (Sandbox Code Playgroud)
一方面和:
try {
a;
block;
off;
statements;
} catch (Exception e) {
handle;
exception;
e;
}
do;
some;
cleanup;
Run Code Online (Sandbox Code Playgroud)
在另一
我经常遇到这样的情况: -
try{
...
stmts
...
}
catch(Exception ex) {
...
stmts
...
} finally {
connection.close // throws an exception
}
Run Code Online (Sandbox Code Playgroud)
最后还需要一个try-catch块.
克服这个问题的最佳做法是什么?
在Java try{} ... catch{} ... finally{}块中,finally{}无论try/catch中发生什么,通常都认为其中的代码是"保证"运行的.但是,我知道至少有两种情况不会执行:
System.exit(0)被召唤; 要么,printStackTrace()退出)是否有任何其他程序行为会阻止finally{}块中的代码执行?代码在什么具体条件下执行?
编辑:正如NullUserException指出的那样,第二种情况实际上并非如此.我认为这是因为标准错误中的文本在标准输出之后打印出来,防止文本在没有向上滚动的情况下被看到.:) 道歉.
除了语法之外,有什么区别
try {
}
catch() {
}
finally {
x = 3;
}
Run Code Online (Sandbox Code Playgroud)
和
try {
}
catch() {
}
x = 3;
Run Code Online (Sandbox Code Playgroud)
编辑:在.NET 2.0中?
所以
try {
throw something maybe
x = 3
}
catch (...) {
x = 3
}
Run Code Online (Sandbox Code Playgroud)
在行为上是等同的吗?
我试图说服自己在finally子句中采取的操作发生在函数返回之前(在内存一致性意义上).从JVM规范,很显然,在一个线程中,程序顺序应该是驱动之前发生关系-如果一个发生b按照程序顺序,然后一 前发生 b.
但是,我还没有看到任何明确说明在返回之前最终发生的事情,那么它呢?或者,编译器是否可以通过某种方式重新排序finally子句,因为它只是简单地记录.
激励示例:我有一个线程从数据库中取出对象并将它们放入ArrayBlockingQueue中,另一个线程将它们取出.我有一些try- finally用于事件计时的块,我看到在log语句之前返回的影响
线程1:
public Batch fetch() {
try {
log("fetch()+");
return queryDatabase();
}
finally {
log("fetch()-");
}
...
workQueue.put(fetch());
Run Code Online (Sandbox Code Playgroud)
线程2:
log("take()+");
Batch b = workQueue.take();
log("take()-");
Run Code Online (Sandbox Code Playgroud)
令我惊讶的是,这是以意想不到的顺序打印出来的.虽然,是的,不同线程中的日志记录语句可能无序出现,但存在至少20毫秒的时间差.
124 ms : take()+
224 ms : fetch()+
244 ms : take()-
254 ms : fetch()-
Run Code Online (Sandbox Code Playgroud)
请注意,这与最终特朗普回归的问题并不完全相同.我不是要问将返回什么,而是询问内存一致性和执行顺序.
在Java中,如果我们在try-catch-finally的try块中插入一个return语句,那么finally块是否会被执行?
我的理解是,如果输入了try,则必须*始终*执行finally子句.
import random
from multiprocessing import Pool
from time import sleep
def Process(x):
try:
print x
sleep(random.random())
raise Exception('Exception: ' + x)
finally:
print 'Finally: ' + x
Pool(3).map(Process, ['1','2','3'])
Run Code Online (Sandbox Code Playgroud)
预期的输出是对于每个由第8行单独打印的x,必须出现'Finally x'.
示例输出:
$ python bug.py
1
2
3
Finally: 2
Traceback (most recent call last):
File "bug.py", line 14, in <module>
Pool(3).map(Process, ['1','2','3'])
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/pool.py", line 225, in map
return self.map_async(func, iterable, chunksize).get()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/pool.py", line 522, in get
raise self._value
Exception: Exception: 2
Run Code Online (Sandbox Code Playgroud)
似乎终止一个进程的异常终止了父进程和兄弟进程,即使在其他进程中还有其他工作 …
我有一个程序如下:
public class Main {
public static void main(String[] args)throws Exception
{
int res = test();
System.out.println("after call , res = " + res) ;
}
public static int test()throws Exception
{
try
{
return 10/0;
}
finally
{
System.out.println("finally") ;
}
}
}
Run Code Online (Sandbox Code Playgroud)
运行以上程序后,在控制台中看到以下结果:
finally
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Main.test(Main.java:17)
at Main.main(Main.java:7)
Run Code Online (Sandbox Code Playgroud)
这种行为是正常的,因为抛出到main方法的异常.
然后我改变代码如下:
public class Main {
public static void main(String[] args)throws Exception
{
int res = test();
System.out.println("after call , res = …Run Code Online (Sandbox Code Playgroud) 使用简单try/finally块编译以下代码时,Java Compiler将生成以下输出(在ASM Bytecode Viewer中查看):
码:
try
{
System.out.println("Attempting to divide by zero...");
System.out.println(1 / 0);
}
finally
{
System.out.println("Finally...");
}
Run Code Online (Sandbox Code Playgroud)
字节码:
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
INVOKEVIRTUAL java/io/PrintStream.println (I)V
L3
LINENUMBER 12 L3
GOTO L4
L1
LINENUMBER 14 L1
FRAME SAME1 java/lang/Throwable
ASTORE 1
L5
LINENUMBER 15 L5
GETSTATIC java/lang/System.out : …Run Code Online (Sandbox Code Playgroud)