pap*_*ski 0 java postgresql swing intellij-idea
这个程序是关于自动完成的.当我输入内容时textfield,会出现一系列建议.
onWordUpdated()当我输入内容时,我为DB提供了一个建议列表的方法textfield.
现在,问题是我有这个错误:
exception java.sql.SQLException is never thrown in body of corresponding try statement
Run Code Online (Sandbox Code Playgroud)
我在代码中发表了评论,以便您知道哪一行.
有人可以帮我解决这个问题吗?
谢谢..
我有这个代码:
public void onWordUpdated(final String toComplete)
{
new Thread(new Runnable()
{
public void run()
{
try
{
final List<Suggestion> suggestions = suggestor.getSuggestions(toComplete);
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
try
{
suggestionWidgetModel.clear();
for (Suggestion suggestion : suggestions)
suggestionWidgetModel.addElement(suggestion.getCaption());
if (!suggestions.isEmpty())
suggestionWidget.setSelectedIndex(0);
}
catch (SQLException e) // This line is my problem, Could someone help me how to fix this? Thanks..
{
e.printStackTrace();
}
}
});
}
catch (SQLException e1)
{
onSqlError(e1);
}
}
}, "onWordUpdated").start();
}
Run Code Online (Sandbox Code Playgroud)
Java有两种类型的例外:未经检查(从RuntimeException或继承Error)和检查(所有其他从中继承Exception).
已检查的异常具有以下属性:
Exception.throws SomeException,该代码也必须在try-catch中,或者其方法也必须指定throws SomeException.由于前两个检查,编译器可以检测是否实际可以在某个代码块中抛出已检查的异常.结果,这导致了第三个属性:
Exception中不能出现的类型,则会try生成编译错误.编译器主要是为了告诉你你犯了一个错误:你正在处理一个永远不会被抛出的异常. SQLException是一个经过检查的例外,因此它受这些规则的约束.下面的try块中没有任何代码行(或它们调用的方法)都可以抛出一个,SQLException因此编译器会通过编译错误告诉您.
try {
suggestionWidgetModel.clear();
for (Suggestion suggestion : suggestions)
suggestionWidgetModel.addElement(suggestion.getCaption());
if (!suggestions.isEmpty())
suggestionWidget.setSelectedIndex(0);
}
catch (SQLException e) // This line is my problem, Could someone help me how to fix this? Thanks..
{
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
编译器只是告诉您此时不需要捕获该异常.
SQLException是一个经过检查的异常,这意味着如果您明确地抛出它,或者您调用一个在其throws子句中声明它的方法,您的代码应该只能看到它.这些特定的try/catch块中的代码都不适用.
你应该能够摆脱内部的try/catch块,也可能是外层的.
IIRC,理论上可以看到尚未声明的已检查异常,但除非您采取特殊措施才能实现,否则不太可能出现这种情况.
| 归档时间: |
|
| 查看次数: |
9644 次 |
| 最近记录: |