4

I need to separate SQLExceptions according to ErrorCode. I have the following if statement in catch block but the else condition is printed always.

catch (SQLException ex) {

      if (ex.getErrorCode() == 28502){
         System.out.println("Username Problem");
      }

      else{
         System.out.println("Other Problem");
      }  

     Logger.getLogger(FirstTimeMainFrame.class.getName()).log(Level.SEVERE, null, ex);

}

Actually, when I enter unexpected username when creating DB, the following SQLExceptions are thrown.

java.sql.SQLException: Failed to create database 'myDB', see the next exception for details.
//rest of exception.
Caused by: ERROR XJ041: Failed to create database 'myDB', see the next exception for details.
//rest of exception.
Caused by: ERROR XBM01: Startup failed due to an exception. See next exception for details
//rest of exception.
Caused by: ERROR 28502: The user name 'AAA.AAA' is not valid.
//rest of exception.

But my catch is always print Other Problem. How can I separate different SQLExceptions according to last ErrorCode?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Black White
  • 700
  • 3
  • 11
  • 31
  • 1
    Judging by the exception, it's actually the third chained exception that has that code, so try traversing the chain using `ex.getNextException()`. Loop until that's `null` or you find the desired error code. – blm Dec 19 '15 at 00:01
  • can you just do a System.out.println(ex.getErrorCode())? that can clearly tell you whats the code returned – ajay.patel Dec 19 '15 at 00:06
  • @zerocool it returns `40000` – Black White Dec 19 '15 at 00:08
  • cool.. compare it with 40000.. – ajay.patel Dec 19 '15 at 00:08
  • @zerocool I'd guess *any* `Failed to create database` exception will have an error code of 40000, so that won't help differentiate them. – blm Dec 19 '15 at 00:18

2 Answers2

1

--- SOLVED ---

Thanks for the routed comments for @blm first.

Trick is; Other coming Exceptions (1st and 2nd) has String values in their SQLState only numeric value in their ErrorCode.

SQLException chain 3rd exception has 28502 both SQLState and ErrorCode. This is the difference between 1st and 2nd Exceptions i think.

So i have changed my

Catch Block;

catch (SQLException se) {

      do {

         if(se.getSQLState().equals("28502")){
            System.out.println("Username Problem");
         }

         else{
            System.out.println("Other Problem");
         }  

      } while ((se = se.getNextException()) != null);

}

Output is;

Other Problem
Other Problem
Username Problem
Black White
  • 700
  • 3
  • 11
  • 31
  • I'm glad you figured out an approach that works for you. You might want to look at https://issues.apache.org/jira/browse/DERBY-6779 and possibly file a related issue describing your scenario and the workaround you had to use, so the Derby team can investigate making this process easier in the future. – Bryan Pendleton Dec 19 '15 at 15:56
0

you should get the cause exceptions by using the 'getCause()' method of the SqlExeption object that was caught. then, you can cast the result to Exception and check its error code.
check this for more info: Throwable.getCause()

Edit:

SQLException temp = ex;
while(temp.getCause() != null)
{
     temp = (Exception)temp.getCause();
}
//  check if temp is SQLException and if it is check ((SQLException)temp).getErrorCode() == whatever you want
Victor Bouhnik
  • 198
  • 2
  • 14
  • `getCause()` returns `ERROR XJ041: Failed to create database 'myDB', see the next exception for details.` We need follow the chain i think. But how. – Black White Dec 19 '15 at 00:10
  • right, and then you should keep calling it till you get to the one with the right status code – Victor Bouhnik Dec 19 '15 at 00:11
  • But exact errorcode will always showing last in chain for `SQLExceptions` i dont know. – Black White Dec 19 '15 at 00:12
  • This is basically what I was suggesting in my comment on the original post, although I don't know offhand the difference between `getCause()` and `getNextException()`, so try both. :-) Whichever works, the `getErrorCode()` on the last exception should give you the root cause. – blm Dec 19 '15 at 00:20
  • `getErrorCode()` not working for `temp` Should it be `SQLException` ? – Black White Dec 19 '15 at 00:21
  • yes, check if it can be cast to SQLException, and if it does, cast it and check its getErrorCode() – Victor Bouhnik Dec 19 '15 at 00:22
  • 1
    @BlackWhite Maybe, give it a try. `getNextException()` already returns an `SQLException`, so maybe it returns the same as `getCause()` but without requiring the cast? – blm Dec 19 '15 at 00:23
  • Ok i changed the object to `SQLException temp = ex;` and while loop to `temp = (SQLException) temp.getCause();` and if statement is `temp.getErrorCode() == 28502` But this time i dont have any printout :) – Black White Dec 19 '15 at 00:27
  • changed it with your fix – Victor Bouhnik Dec 19 '15 at 00:29
  • @blm I was try using `getNextException()` method in many ways but i am not succeed. – Black White Dec 19 '15 at 00:35
  • Also if i add `System.out.println(temp.getCause());` inside while loop. I receive two times `ERROR XJ041: Failed to create database 'myDB', see the next exception for details.` And never enter to if or else statement. – Black White Dec 19 '15 at 00:43
  • might be you have another exception while casting to SQLException, I would suggest that you debug and check what is the **type** of the root cause exception. – Victor Bouhnik Dec 19 '15 at 00:49
  • When we add while loop inside catch block i have not any Logger output or if else statement output. I have set try catch block for while loop. Problem in while loop, it is throw `java.lang.ClassCastException: org.apache.derby.iapi.error.StandardException cannot be cast to java.sql.SQLException` But i dont know maybe i use wrong catch mechanism for catch the while loop Exceptions. I was use `catch(Exception es)` for catch the while loop exception. – Black White Dec 19 '15 at 01:24