aliteralmind
aliteralmind

Reputation: 20163

Should the "calling function" be the prefix in a "good" exception message?

I have always put the name of the "calling function" as the prefix for all exception messages.

throw  new IllegalArgumentException("setTheNumber: i_number (" + i_number + ") must be greater than zero.");

I think I originally started doing this when viewing a log years ago, which did not have anything except the exception's getMessage() (no stack trace).

Another possible situation is when an exception message should appear to be thrown by a function other than it actually was (one more relevant to the user of my library, for instance). I'm not sure that's valid enough, however.

Is it a good practice to prefix every exception message with the calling function? Or should it just be expected that the stack trace is always made available?

Thank you for any advice.

Upvotes: 0

Views: 247

Answers (1)

user1339772
user1339772

Reputation: 803

Its better to log the complete stack trace in case of an error and not worry about the method names. Exceptions should be thrown in exceptional situations - when something really went wrong and it is always better to log the entire trace, which would help to debug the issue.

Upvotes: 5

Related Questions