Reputation: 2148
I am using commons logging in a Java application, and I want to log the exception from the stack trace.
catch( Exception exception ) {
logger.error( "FailedCreateActivityFunction Exception Occured : " , exception );
throw new EngineException( getMessage( ERROR_FailedCreateActivityFunction, FunctionName ), exception );
}
Will this throw the exception twice ? If yes how to fix it ?
Whats the difference between using it as this way using + exception instead of , exception
logger.error( "FailedCreateActivityFunction Exception Occured : " + exception );
Upvotes: 2
Views: 10058
Reputation: 513
No it will not throw exception twice. This code will throw only EngineException.
You should avoid logging and throwing exceptions, do only one thing. Log and throw is an antipattern. You can read more about exception handling here: Exception Handling Antipatterns.
Difference between those two ways of executing error method is:
Upvotes: 5
Reputation: 50281
No, it won't throw it twice.
The first method will print your message AND the exception's stacktrace.
The second method will print only your message AND the exception message (invoking throwable.toString() that returns a short description of the error), without the stacktrace.
Upvotes: 1