开发者

How to fix SQLException in the Catch Statement?

This program is about auto complete. When I type something to the textfield, a list of suggestions will appear.

I make the method onWordUpdated() for a list of suggestions from the DB when I type something to the textfield.

Now, the problem is I have this error:

exception java.sql.SQLException is never thrown in body of corresponding try statement

I made a comment in the code so that you will know which line.

Could someone help me how to fix this?

thanks..

I have this code:

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.setSelect开发者_JAVA百科edIndex(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();
  }


The compiler is simply telling you that you don't need to catch that exception at that point.

SQLException is a checked exception, which means that your code should only see it if you either explicitly throw it, or you call a method that declares it in its throws clause. Neither of these is true for the code in that particular try/catch block.

You should be able to just get rid of the inner try/catch block and probably the outer one too.


IIRC, it is theoretically possible to see checked exceptions that haven't been declared, but this unlikely to arise unless you take special steps to make it happen.


Java has two types of exceptions: unchecked (those that inherit from RuntimeException or Error) and checked (all others that inherit from Exception).

A checked exception has the following properties:

  • If a block of code throws one, it must be caught in a catch block or the method must declare that it may throw that type of Exception.
  • If some code calls a method that throws SomeException, that code must also be in a try-catch or its method must also specify throws SomeException.

Because of the first two checks, the compiler can detect whether a checked exception can actually be thrown in a certain block of code. As a result, this leads to a third property:

  • If the catch clause of a try-catch block declares an Exception type that cannot occur in the try block, then a compile error is generated. The compiler does this primarily to tell you that you've made an error: you're dealing with an exception that will never be thrown.

SQLException is a checked exception so it is subject to those rules. None of the lines of code (or the methods they call) in the try block below can ever throw a SQLException so the compiler tells you via a compile error.

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();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜