Java - find the first cause of an exception

后端 未结 10 877
渐次进展
渐次进展 2020-12-09 01:50

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

相关标签:
10条回答
  • 2020-12-09 02:23

    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

    0 讨论(0)
  • 2020-12-09 02:24

    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.

    0 讨论(0)
  • 2020-12-09 02:26

    If you are already on Guava than Throwables.getRootCause() comes to the rescue.

    0 讨论(0)
  • 2020-12-09 02:29

    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.

    0 讨论(0)
提交回复
热议问题