1

I want catch that exception but it doesn't work. Basically what it does the follow code is call method createConnection() for try to make the connection to the database. If the database doesn't exist throws two exception. I catch both, but only works for the SQLException and not for the 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);
        }
    }

the code from createConnection()

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

and the exception is

    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)
Jota_sk
  • 169
  • 2
  • 14
  • How do you know its not catched even printstrace will print strace and you doing system.exit in code which stop program. – Panther Apr 17 '15 at 14:41
  • Are `HsqlException` and `org.hsqldb.HsqlException` the same class? What does your import for `HsqlException` say? – Robert Bain Apr 17 '15 at 14:43
  • Are not you getting any compilation error? And I did not get why u have called createConnection(); in top try block and also in finally's try block? – Bacteria Apr 17 '15 at 14:55
  • I knowed it because it throws the same exception two times, one for each try. And in the firts try I doesn't do printstrace. Even removing printstrace keep throwing the exception. – Jota_sk Apr 17 '15 at 17:11
  • My import say "import org.hsqldb.HsqlException;". I don't have any compilation error. I call the method two time, because the method create a connection to a database, if the database doesn't exist throw a exception and I call the class createDB, when it finish try again to make the contact to the database and if is not created properly I want to close the program, because without database can't work the program – Jota_sk Apr 17 '15 at 17:17

2 Answers2

0

You need to change your createConnection() method. You don't want to throw an exception there.

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);
    }
}
mvlaicevich
  • 103
  • 1
  • 8
  • Thanks for the answer. The first time is called createConnection(); throw the exception org.hsqldb.HsqlException when doesn't affect the e.printStackTrace() on the second try. But I removed the last one and keep throwing the exception. Also I try to catch Exception and keep throwing the exception – Jota_sk Apr 17 '15 at 17:09
  • I see, you need to make a try catch on createConnection() instead of throwing exceptions , so you catch the message and doesn't throw that exception. The exception is throwing inside createConnection(); I edited my response – mvlaicevich Apr 17 '15 at 17:19
  • Thanks. And if I want check if the database exist or not for create a new one? Because that's why I throw exception in the method for catch on the constructor. And it create the database if fail and is called again. – Jota_sk Apr 17 '15 at 17:32
  • take a look at this http://stackoverflow.com/questions/3148092/java-jdbc-mysql-connector-how-to-resolve-disconnection-after-a-long-idle-time – mvlaicevich Apr 17 '15 at 18:25
0

This is not an exception thrown by the method you are calling.

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)

It is a Java logger message generated internally by HSQLDB and in this case it is sent by default to the stderr which happens to be the console. See the Guide on how to handle the logger messages.

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

fredt
  • 24,044
  • 3
  • 40
  • 61