Rulabor
Rulabor

Reputation: 13

How to log an exception declared in method signature

public void addTitleRow() throws SQLException{
    ResultSet rs = stmt.executeQuery("SELECT * FROM ANY")
    log.info ? }

How can I log an exception declared to be thrown in the method signature? I'm using log4j2

Upvotes: 0

Views: 119

Answers (1)

ninja.coder
ninja.coder

Reputation: 9658

You'll have to catch the exception first in order to to log it. You can suppress it or simply re-throw it later if you wish to.

Here is the code snippet:

Re-Throw Exception:

try {
    /* Do Something Here */
} catch (Exception ex) {
    log.error("An Exception Has Occurred!", ex);
    throw ex;
}

Suppress Exception:

try {
    /* Do Something Here */
} catch (Exception ex) {
    log.warn("An Exception Has Occurred!", ex);
}

Upvotes: 1

Related Questions