Java setOnAction订阅引发异常的事件 [英] Java setOnAction subscribe to event that throws exception
问题描述
首先请允许我说一下,我不是Java程序员,并且到目前为止还无法理解为什么会这样.
Let me preface this by saying, I am not a Java programmer and have thus far been unable to understand WHY this is the case.
我目前正在从事一项作业,要求我使用JavaFX创建基本的GUI.此GUI的功能要求单击某些按钮才能执行CRUD操作.目前,我已在我的Insert方法中正确设置了所有内容:
I'm currently working on a homework assignment that requires me to create a basic GUI using JavaFX. The functionality of this GUI requires that there are buttons that will perform CRUD operations when clicked. Currently I have everything set up properly in my Insert method:
public void Insert() throws SQLException{
//Insert new record here
}
但是,每当我尝试使用'setOnAction'订阅此方法时,编译器就会告诉我该事件存在未处理的异常:
However, whenever I try to subscribe to this method using 'setOnAction', the compiler is telling me there's an unhandled exception on the event:
btnInsert.setOnAction(e ->Insert());
我想知道是否有一种相对简洁的方式来解决这个问题?到目前为止,我一直无法提出解决方案.
I'm more curious if there's a way to handle this in a relatively succinct way? I've thus far been unable to come up with a solution.
推荐答案
这是因为Java要求您声明所有引发的检查异常-因此,每当调用可能引发检查异常的方法时,都必须捕获该异常或声明你可以自己扔.
That's because Java requires you to declare all checked exceptions you throw - so whenever you call a method that may throw a checked exception, you must either catch it or declare you may throw it yourself.
请参见此问题,以获取有关已检查和未检查的异常的说明(简短版本-未选中继承Error
或RuntimeException
的任何Exception,而所有其他异常都被选中.)
See this question for an explanation of checked vs. unchecked exception (The short version - any Exception which inherits either Error
or RuntimeException
is unchecked, while all other exceptions are checked.)
When you are providing setOnAction
with a lambda, you are actually creating an anonymous class implementation of EventHandler<ActionEvent>
. Since it does not declare it throws any exceptions, neither does your anonymous class (and in fact - it can't).
因此,您有两种解决问题的方法:
So you have two options of solving the problem:
-
捕获并处理异常:
Catch and handle the exception:
btnInsert.setOnAction(e -> {
try {
Insert();
} catch (SQLException ex) {
// Log error, show error message, etc... Whichever is applicable for your application
}
});
抛出未检查的异常:
Rethrow an un-checked exception:
btnInsert.setOnAction(e -> {
try {
Insert();
} catch (SQLException ex) {
throw new RuntimeException(ex); // Or any other subclass of RuntimeException or Error
}
});
关于在两个选项之间进行选择- Oracle的文档这样说:
As to choosing between the two options - Oracle's documentation says this:
这是最重要的原则:如果可以合理地期望客户端从异常中恢复,请将其设置为已检查的异常.如果客户端无法采取任何措施来从异常中恢复,请将其设置为未经检查的异常.
Here's the bottom line guideline: If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception.
当然,所有这些都可以从实际的EventHandler
移开,因此可以在没有检查异常的方法上调用它.
Of course, this could all be moved away from the actual EventHandler
, so it can be called on a method that is checked-exception-free.
这篇关于Java setOnAction订阅引发异常的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!