jiafu
jiafu

Reputation: 6556

if every exception catch should log it?

some books mentioned that the followed mode is bad. It says every exception if be rethrowed shouldn't log it to avoid to dupliacte exception log.? any other issues?

I am confused that if I can't log any exception when rethrow it , if the issue exist?

or if I log it, I am confused if the too many log generated if everybody do it.

catch (NoUserException e) { 

    LOG.error("No user available", e); 

    throw new UserServiceException("No user available", e); 

} 

the reference http://today.java.net/pub/a/today/2006/04/06/exception-handling-antipatterns.html#logAndThrow

Upvotes: 2

Views: 134

Answers (3)

Maciej Ziarko
Maciej Ziarko

Reputation: 12104

As far as exceptions are concerned, the most important log message should be located in service layer. Important thing is keeping the whole stack trace so the issue can be easily located even after several rethrows. You can always put logs in all layers and manipulate logging level for certail layers to see only logs from layer you are currently debugging/working on. Other logs can be set to OFF. Read documentation for your favorite logger to learn more about that.

Upvotes: 0

Chau Chi Thien
Chau Chi Thien

Reputation: 31

Every LOG function have a switch to disable that log message so you have to LOG all exception if it is unexpected one. If you expected that exception, for example you check if the String is a number and you would like to know the result on exception, then you do not need to do the Log.

Upvotes: 0

Nir Alfasi
Nir Alfasi

Reputation: 53545

I'm not sure about the books you mentioned, but to me, as someone who'll have to debug the code and find the root cause of the bugs, I'd like to read about it later in the logs as close as possible to the place where it first triggered.

Upvotes: 2

Related Questions