未报告的异常 java.sql.SQLException; 必须被捕获或声明被抛出?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4457352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 17:47:44  来源:igfitidea点击:

Unreported exception java.sql.SQLException; must be caught or declared to be thrown?

java

提问by karikari

I got this error while trying to compile the below code. I would like to know what is I have done wrong.

尝试编译以下代码时出现此错误。我想知道我做错了什么。

unreported exception java.sql.SQLException; must be caught or declared to be thrown
 Class.forName(myDriver);

               ^
private void setupInfo() {

    Driver driver = new org.gjt.mm.mysql.Driver();
    String url = "jdbc:mysql://localhost:3306/test";
    String username = "root";
    String password = "123456";

    String problemFeatureSpecTableName = "ProblemFeatureSpec";
    String solutionFeatureSpectTableName = "SolutionFeatureSpec";
    String classTableName = "Class";
    String extraDataTableName = "ExtraData";
    String casebaseTablename = "CaseBase";
    String problemTableName = "Problem";
    String solutionTableName = "Solution";
    String inactiveContextsTableName = "InactiveContext";
    String constantsTableName = "Constants";
    dbInfo = new DBInfo(new JDBCDriverInfo(driverName, url, username, password),constantsTableName);
    problemSpecInfo = new FeatureSpecRDBInfo(problemFeatureSpecTableName, classTableName, extraDataTableName);
    solutionSpecInfo = new FeatureSpecRDBInfo(solutionFeatureSpectTableName, classTableName, extraDataTableName);
    rdbCasebaseInfo = new RDBCaseBaseInfo(casebaseTablename, solutionTableName, problemTableName, inactiveContextsTableName);
}

采纳答案by Matt Ball

You either need to catchthe exception in your method:

您要么需要在方法中捕获异常:

public void setupInfo()
{
    try
    {
        // call methods that might throw SQLException
    }
    catch (SQLException e)
    {
        // do something appropriate with the exception, *at least*:
        e.printStackTrace();
    }
}

Or declarethe method to throw SQLException:

或者声明抛出的方法SQLException

private void setupInfo() throws SQLException
{
    // call methods that might throw SQLException
}

回答by Bnjmn

This line of code throws an uncaught exception:

这行代码抛出一个未捕获的异常:

Driver driver = new org.gjt.mm.mysql.Driver();

try this:

尝试这个:

try {
   Driver driver = new org.gjt.mm.mysql.Driver();
}
catch (java.sql.SQLException e) {
  // you may want to do something useful here
 // maybe even throw new RuntimException();
}

回答by Pakka Techie

Catch the exception or throw it. Better use an IDE (Eclipse or Netbeans), which will tell you the error the moment you press enter.

捕获异常或抛出它。最好使用 IDE(Eclipse 或 Netbeans),它会在您按下 Enter 键时告诉您错误。

回答by Nowaker

Always try to get help from your IDE. IDEs can often fix the error automatically. Press alt+enter in IntelliJ IDEA or ctrl+1 in Eclipse and choose to fix the error.

始终尝试从您的 IDE 获得帮助。IDE 通常可以自动修复错误。在 IntelliJ IDEA 中按 alt+enter 或在 Eclipse 中按 ctrl+1 并选择修复错误。