Printing Exception vs Exception.getMessage

The first doesn’t compile because the method error accept a String as first parameter and a Throwable as second parameter.

e.getMessage() is not a Throwable.

The code should be

} catch (SomeException e) {
    // No stack trace
    logger.error("Noinstance available! " + e.getMessage());
}

Compared with

} catch (SomeException e) {
    // Prints message and stack trace
    logger.error("Noinstance available!", e);
}

The first prints only a message. The second prints also the whole stack trace.

It depends from the context if it is necessary to print the stack trace or not.

If you already know why an exception can be thrown it is not a good idea to print the whole stack trace.

If you don’t know, it is better to print the whole strack trace to find easily the error.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)