kish
kish

Reputation: 85

How to log a sql exception?

I am trying to log the stack trace in the below way it is working fine but i am trying to figure out is there any other efficient way to do for logging the exception?

 catch (SQLException e) {
                LOG.error(
                          "Sql Exception: "  
                                   + e.getStackTrace()[1].getClassName() + "." 
                                   + e.getStackTrace()[1].getMethodName(), e);
                throw e;

Upvotes: 1

Views: 2913

Answers (2)

Vivek Singh
Vivek Singh

Reputation: 2073

You don't need to use + e.getStackTrace()[1].getClassName() + "." + e.getStackTrace()[1].getMethodName() in your code, this information is generated by the logger itself using the Exception stack trace.

Upvotes: 0

janos
janos

Reputation: 124824

Yes:

LOG.error("Sql Exception", e);

Notice the second parameter e. When the Exception/Throwable is passed in like this, loggers include the stack trace. No need to work with e.getStackTrace() manually (and awkwardly).

Upvotes: 2

Related Questions