views:

524

answers:

7

I need to check if an exception is caused by some database problem. I receive an Exception and check if its cause contains the "ORA" string and return that (something like "ORA-00001"). The problem here is that the exception I receive is nested inside other exceptions so if I don't find if it's an oracle exception I have to check into the cause of that exception and so on. There is a more clean way to do this? There is a way to know the first cause (the deep-nested exception) of a given exception?

My current code looks this way:

private String getErrorOracle(Throwable e){
     final String ORACLE = "ORA";
     if (e.getCause() != null && e.getCause().toString().contains(ORACLE)){
      return e.getCause().toString();
     } else if(e.getCause() != null){
      return getErrorOracle(e.getCause());
     } else {
      return null;
     }
    }

Thanks.

+2  A: 

Just traverse the exception chain until you get to an exception with no cause, and then just return that message, if you want the last one.

Your function will only get the first cause, if there is one.

You may want to look at finding the first cause in your package though, as the actual deepest one may be an oracle exception, which is helpful, but unless you can see where you created the problem, you will have a hard time fixing it.

James Black
Thanks. I think I'll go this way then. I have to do this because the cliente want to show some specific messages when the exception is caused by any problem related to the data base. When I receive the exception I don't know if it was caused by the data base since the first exception is nested inside the exception chain so I was looking for a way to solve this.
Averroes
ehm, his method is recursive, hence he will get the root cause.
Bozho
@Bozho - He has three if statements with returns. If he had it call itself again then it would be recursive, but, he would need to change his logic, so it will exit.
James Black
The second if statement does call itself and the logic looks right, I don't see why this would not find the right exception.
vickirk
A: 

you can use the getStackTrace() from the Throwable class. This would give you the stack of StackTraceElements to work with. You can iterate through the StackTraceElements[] to find "ORA" string.

Let me know if you need an example.

coolest_head
The stack trace only shows the names of the methods that were called on the way "down"; it won't show the messages of any nested exceptions, which is what Averroes is looking for.
Andrzej Doyle
You are right. edited.
coolest_head
+1  A: 

I think that any error that is thrown by oracle will be wrapped in a SQLException (somebody please correct me if wrong). Once you have accessed the SQLException you should be able to call

getErrorCode() Retrieves the vendor-specific exception code for this SQLException object.

Let me know if this works as I have never tried it :-)

Karl

Karl
Yes. It seems that the exception thrown by Oracle extends SQLException. Regarding getErrorCode() this seems to return a number and I guess you're right about this. I'll check it tomorrow at work. Thanks.
Averroes
A: 

If the exception being thrown is always going to be of a specific type, like OracleException, you can catch just that exception.

For example:

`try {

...

} catch(OracleException oe) {

...

}`

This would only apply if there are specific Oracle exceptions being thrown. I don't know much about Oracle, so before attempting this you will probably want to find out if that's what's happening.

spork
+1  A: 

You could improve your code checking for SQLException

import java.sql.SQLException;

private static final String ORACLE = "ORA";

public String doHandle(Throwable t) {
    if (t.getClass().isAssignableFrom(SQLException.class)) {
    SQLException e = (SQLException) t;
    int errCode = e.getErrorCode();
    String state = e.getSQLState();
    String msg = e.getMessage();
    if (msg.contains(ORACLE)) {
     return msg;
     }
    } else {
     if (t.getCause() != null) {
      return this.doHandle(t.getCause());
      }
     }
    return "";
}

Also, I think in Oracle "errCode" contains the number associated to ORA-nnnn

JuanZe
Thanks. This help me a lot to improve the function. I just changed "t.getClass().isAssignableFrom(SQLException.class)" to "t instaceof SQLException" because the first one wasn't being evaluated true (The exception I was testing were BatchUpdateException)
Averroes
+1  A: 

Probably a bit overkill for your usage but I think it is cleaner (and reusable)

interface ThrowablePredicate {
    boolean accept(Throwable t);
}

public OracleErrorThrowablePredicate implements ThrowablePredicate {
    private static final ORA_ERR = "ORA";

    public boolean accept(Throwable t) {
     return t.toString().contains(ORA_ERR);
    }
}


public class CauseFinder {

   private ThrowablePredicate predicate;

   public CauseFinder(ThrowablePredicate predicate) {
      this.predicate = predicate;
   }

   Throwable findCause(Throwable t) {
      Throwable cause = t.getCause();

      return cause == null ? null 
         : predicate.accept(cause) ? cause : findCause(cause)
   }
}


// Your method
private String getErrorOracle(Throwable e){
    return new CauseFinder(new OracleErro

rThrowablePredicate()).findCause(e); }

vickirk
+1  A: 

In the interests of not reinventing the wheel, if you're using Apache Commons Lang, then look at ExceptionUtils.getRootCause().

Is it worth including a library just for that? Maybe not. But if you already have it on your classpath, it's there for you, and note that it does some things that a 'naive' implementation might not do (e.g. deal with cycles in the cause chain... ugh!)

Cowan