捕获 HsqlException

Catch HsqlException

我想捕获那个异常,但它不起作用。基本上它所做的后续代码是调用方法 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)

您需要更改 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);
    }
}

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

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)

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

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