我知道涉及在try/catch/finally块中返回的令人头痛的问题 - finally中的返回始终是方法的返回,即使try或catch块中的返回应该是执行的返回.
但是,同样适用于System.exit()?例如,如果我有一个try块:
try {
//Code
System.exit(0)
}
catch (Exception ex) {
//Log the exception
}
finally {
System.exit(1)
}
Run Code Online (Sandbox Code Playgroud)
如果没有异常,将调用哪个System.exit()?如果exit是return语句,则System.exit(1)行将始终(?)被调用.但是,我不确定退出的行为是否与返回不同.
代码处于极端情况下,即使不是不可能,也很难重现,因此我无法编写单元测试.我今天晚些时候会尝试进行一项实验,如果我得到一些免费的分钟,但我很好奇,也许有人知道答案,可以提供它,或者我无法运行实验.
Eclipse在以下代码中给出了警告:
public int getTicket(int lotteryId, String player) {
try {
c = DriverManager.getConnection("jdbc:mysql://" + this.hostname + ":" + this.port + "/" + this.database, this.user, this.password);
int ticketNumber;
PreparedStatement p = c.prepareStatement(
"SELECT max(num_ticket) " +
"FROM loteria_tickets " +
"WHERE id_loteria = ?"
);
p.setInt(1, lotteryId);
ResultSet rs = p.executeQuery();
if (rs.next()) {
ticketNumber = rs.getInt(1);
} else {
ticketNumber = -1;
}
ticketNumber++;
p = c.prepareStatement(
"INSERT INTO loteria_tickets " +
"VALUES (?,?,?,?)");
p.setInt(1, lotteryId);
p.setInt(2, ticketNumber);
p.setString(3, player); …Run Code Online (Sandbox Code Playgroud) 请参阅以下代码并解释输出行为.
public class MyFinalTest {
public int doMethod(){
try{
throw new Exception();
}
catch(Exception ex){
return 5;
}
finally{
return 10;
}
}
public static void main(String[] args) {
MyFinalTest testEx = new MyFinalTest();
int rVal = testEx.doMethod();
System.out.println("The return Val : "+rVal);
}
}
Run Code Online (Sandbox Code Playgroud)
结果是返回Val:10.
Eclipse显示警告:finally block does not complete normally.
catch块中的return语句会发生什么?
这是一个采访问题:
public class Demo {
public static void main(String[] args) {
System.out.println(foo());
}
static String foo() {
try {
return "try ...";
} catch (Exception e) {
return "catch ...";
} finally {
return "finally ..."; //got as result
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是为什么没有编译时错误.当我在finally块中有return语句时,它必然会从而finally不是try和catch块返回.我尝试使用-Xlint选项编译此代码,它会发出警告.
warning: [finally] finally clause cannot complete normally
Run Code Online (Sandbox Code Playgroud) 我需要一种方法来打破try/catch块的中间而不抛出异常.类似于break并继续for循环的东西.这可能吗?
关于抛出一个自定义异常(命名为"BreakContinueException"),我一直在做一些奇怪的事情,这个异常在catch处理程序中什么都不做.我确定这很扭曲.
那么,我不知道任何直接的解决方案?
我试图使用JDK 7的"try-catch with resources"声明; IntelliJ强调我的资源线,说
此语言级别不支持尝试使用资源.
当我尝试编译时,我得到:
java:在-source 1.6中不支持try-with-resources(使用-source 7或更高版本来启用try-with-resources)
我检查了我的当前项目是否启用了try-with-resources,我的项目是使用JDK 7(Library:C:\ Program Files\Java\jdk1.7.0_11).有任何想法吗?我无法弄清楚要改变什么选项(如果这甚至是问题).
java try-catch intellij-idea try-catch-finally try-with-resources
在C#,尝试捕获最终如何阻止工作?
所以如果有异常,我知道它将跳转到catch块然后跳转到finally块.
但是,如果没有错误,catch块将不会运行,但finally块是否会运行呢?
我看到Finallyin Try .. Catch将始终在try catch块执行的任何部分之后执行.
是否有任何不同,只是跳过该Finally部分,然后在try catch块之外运行它?
Try
'Do something
Catch ex As Exception
'Handle exception
Finally
'Do cleanup
End Try
Run Code Online (Sandbox Code Playgroud)
Try
'Do something
Catch ex As Exception
'Handle exception
End Try
'Do cleanup
Run Code Online (Sandbox Code Playgroud) 我在解决一些看起来像这样的代码时遇到了非常痛苦的故障排除经验:
try {
doSomeStuff()
doMore()
} finally {
doSomeOtherStuff()
}
Run Code Online (Sandbox Code Playgroud)
这个问题很难解决,因为doSomeStuff()引发了异常,这又导致doSomeOtherStuff()也抛出异常.第二个异常(由finally块抛出)被抛到我的代码中,但它没有第一个异常的句柄(从doSomeStuff()抛出),这是问题的真正根本原因.
如果代码说了这个,那么问题就很明显了:
try {
doSomeStuff()
doMore()
} catch (Exception e) {
log.error(e);
} finally {
doSomeOtherStuff()
}
Run Code Online (Sandbox Code Playgroud)
所以,我的问题是:
是没有任何catch块使用的finally块是一个众所周知的java反模式?(这显然是一个不太明显的子类,显然是众所周知的反模式"不要狼吞虎咽的例外!")
我有一个工作流方法,并在发生错误时抛出异常.我想将报告指标添加到我的工作流程中.在下面的finally块中,有没有办法判断try/catch块中的一个方法是否引发了异常?
我可以添加自己的catch/throw代码,但更喜欢更清晰的解决方案,因为这是我在项目中重用的模式.
@Override
public void workflowExecutor() throws Exception {
try {
reportStartWorkflow();
doThis();
doThat();
workHarder();
} finally {
/**
* Am I here because my workflow finished normally, or because a workflow method
* threw an exception?
*/
reportEndWorkflow();
}
}
Run Code Online (Sandbox Code Playgroud)