捕获HsqlException

时间:2015-04-17 14:33:42

标签: java hsqldb

我想抓住那个例外,但它不起作用。基本上它的作用是跟随代码是调用方法createConnection(),用于尝试建立与数据库的连接。如果数据库不存在则抛出两个异常。我同时捕获了两个,但只适用于SQLException而不适用于HsqlException

    try {
        createConnection();
    } catch (HsqlException | SQLException e1) {
        System.out.println("Not exist the DataBase. Creating a new one.");
        new CreateDB();
    }finally{
        try {
            createConnection();
        } catch ( SQLException | org.hsqldb.HsqlException e) {
            e.printStackTrace();
            System.out.println("Some big error ocurred. Please contact me.");
            System.exit(0);
        }
    }

createConnection()

中的代码
void createConnection() throws SQLException, org.hsqldb.HsqlException{
    conn = DriverManager.getConnection(URL + DB_FILE +";ifexists=true");
}

,例外是

    2015-04-17T15:12:37.834+0100  SEVERE  could not reopen database
org.hsqldb.HsqlException: Database does not exists: db\dogsRus
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.persist.Logger.open(Unknown Source)
    at org.hsqldb.Database.reopen(Unknown Source)
    at org.hsqldb.Database.open(Unknown Source)
    at org.hsqldb.DatabaseManager.getDatabase(Unknown Source)
    at org.hsqldb.DatabaseManager.newSession(Unknown Source)
    at org.hsqldb.jdbc.JDBCConnection.<init>(Unknown Source)
    at org.hsqldb.jdbc.JDBCDriver.getConnection(Unknown Source)
    at org.hsqldb.jdbc.JDBCDriver.connect(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at ac.uk.jov2.dogsRus.database.DataBaseUtil.createConnection(DataBaseUtil.java:90)
    at ac.uk.jov2.dogsRus.database.DataBaseUtil.<init>(DataBaseUtil.java:54)
    at ac.uk.jov2.dogsRus.database.DataBase.<init>(DataBase.java:16)
    at ac.uk.jov2.dogsRus.Application.<init>(Application.java:28)
    at ac.uk.jov2.dogsRus.Application.main(Application.java:388)

2 个答案:

答案 0 :(得分:0)

您需要更改createConnection()方法。你不想在那里抛出异常。

void createConnection(){
  try{
    conn = DriverManager.getConnection(URL + DB_FILE +";ifexists=true");
  } catch ( SQLException | org.hsqldb.HsqlException e) {
        e.printStackTrace();
        System.out.println("Some big error ocurred. Please contact me.");
        System.exit(0);
    }
}

答案 1 :(得分:0)

这不是您正在调用的方法抛出的异常。

  

2015-04-17T15:12:37.834 + 0100 SEVERE无法重新打开数据库   org.hsqldb.HsqlException:数据库不存在:db \ dogsRus       在org.hsqldb.error.Error.error(未知来源)

这是由HSQLDB在内部生成的Java记录器消息,在这种情况下,它默认发送到恰好是控制台的stderr。有关如何处理记录器消息,请参阅“指南”。

http://hsqldb.org/doc/2.0/guide/management-chapt.html#mtc_jdc_logging