0

Providing the code and the output. Searched the internet but didn't find solution. Everything compiles and runs okay, up until "//PROBLEMATIC LINE", where execution goes directly to "//HERE" without any indication, exception or error. I have used this code with the jdbc driver for SQLServer many times and works fine, what am I possibly missing here? In the same application I am also using this code with mySql jdbc driver and again, works fine. Just in this piece of code it is that there is a problem. Obviously I am using java.sql.*; .

try {
        Class.forName(driver);
        setResponse("Failure. 1");
        connectionTest = DriverManager.getConnection(url, username, password);
        setResponse("Failure. 2");
        String query = "SELECT TOP (1) [id] FROM "+ table + ";";
        PreparedStatement statement = connectionTest.prepareStatement(query);
        setResponse("Failure. 3");
        System.out.println(statement);
        ResultSet resultset = statement.executeQuery();  //PROBLEMATIC LINE
        System.out.println("DOES NOT EVEN PRINT THIS");
        setResponse("Failure. 4");
        if (resultset.first()) setResponse("Success.");
        else setResponse("Failure. 5");
        System.out.println("Query success: ");  
        setResponse("Failure. 6");
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    finally {
        try {
            connectionTest.close();  //HERE
            return getResponse();
        }
        catch (Exception e) {
            e.printStackTrace();
            return getResponse();
        }
    }

Output:

------------------------------------
-------------DATABASES--------------
------------------------------------
SQLServerPreparedStatement:1
Failure. 3
------------------------------------
----------TESTS COMPLETED-----------
------------------------------------
Maral Kay
  • 47
  • 10
  • Your output doesn't show "Failure. 1" or "Failure. 2". What's up with that? – nicomp Feb 08 '18 at 10:52
  • Java has 2 types of throwable objects, Exceptions and Errors. Your catch block catches only `Exception` while `Error` might be thrown (although it is not likely). Try catching `Throwable` to see if that is the case (and printStackTrace() will print something useful). – MatheM Feb 08 '18 at 10:55
  • "where execution goes directly to "//HERE" without any indication, exception or error". That is impossible. There is an exception, you just don't seem to print it correctly. Where does your `System.err` stream point to? – M. Prokhorov Feb 08 '18 at 11:02
  • @nicomp My output prints only the value returned from the method. – Maral Kay Feb 08 '18 at 11:16
  • Please provide the full exception stacktrace. – Mark Rotteveel Feb 08 '18 at 13:02
  • @MarkRotteveel there was no stacktrace, the reason why is marked as the answer below – Maral Kay Feb 09 '18 at 07:52
  • If you got an "exception in thread main", there must be a stacktrace on the console or in the logs. There is no other way. – Mark Rotteveel Feb 09 '18 at 07:59
  • @MarkRotteveel there was not "exception in thread main" till I did what O.O.Balance suggested below. Now the issue is resolved. – Maral Kay Feb 09 '18 at 08:25

2 Answers2

1

When an exception is thrown, the try block will exit. When the try block exits, the finally block will be executed. Since you have return statements in your finally block, you will never get to the catch block.

Don't return in your finally block. Then the stacktrace will get printed and you can deal with the exception.

O.O.Balance
  • 2,930
  • 5
  • 23
  • 35
  • the `finally` block executes **before** the first `catch` block? – cosh Feb 08 '18 at 11:00
  • Yes, it does. See here: https://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html – O.O.Balance Feb 08 '18 at 11:01
  • @O.O.Balance how can I not return in the finally block... my method must return a statement. If an exception is not thrown, finally must return.. – Maral Kay Feb 08 '18 at 11:09
  • that basically says that `The finally block always executes when the try block exits`. i meant if an exception is thrown in the `try` block. if that's the case, `catch` will be executed and only **then** `finally`, right? See https://stackoverflow.com/questions/3109353/what-comes-first-finally-or-catch-block – cosh Feb 08 '18 at 11:09
  • 1
    @MaralKay How about writing `return getResponse();` _after_ the `finally` block? Then your method will return something whether an exception is caught or not. – O.O.Balance Feb 08 '18 at 11:13
  • @O.O.Balance thank you, I totally forgot to place a return statement *after* the finally block. – Maral Kay Feb 08 '18 at 11:14
0

As the others mentionned, you should do the try-catch thing a little bit different. My recommendation:

try {
    Class.forName(driver);
    setResponse("Failure. 1");
    connectionTest = DriverManager.getConnection(url, username, password);
    setResponse("Failure. 2");
    String query = "SELECT TOP (1) [id] FROM "+ table ;
    PreparedStatement statement = connectionTest.prepareStatement(query);
    setResponse("Failure. 3");
    System.out.println(query);
    ResultSet resultset = statement.executeQuery(); 
    while ( resultset.next() {
        System.out.println("only one result because of 'top (1)': " + rs.getInt(1));
    }
     resultset.close();
     statement.close();
     connectionTest.close();        
} catch { SQLException sqlex) {
    System.out.println(getClass().getName() + " SQLException:  " + sqlex.getMessage();
}  catch (Exception e) {
    System.out.println(getClass().getName() + " GenericException:  " + e.getMessage();
     e.printStrackTrace();
}

And if all the needed libraries are in the classpath, especially the JDBC-Driver, it should go fine

Fredy Fischer
  • 458
  • 3
  • 12