Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where Should Exception Messages be Stored

Since I can't use Microsoft as an example for best practice since their exception messages are stored in resource files out of necessity, I am forced to ask where should exception messages be stored.

I figure it's probably one of common locations I thought of

  • Default resource file
  • Local constant
  • Class constant
  • Global exception message class
  • Inline as string literals
like image 768
Orion Adrian Avatar asked Jan 23 '09 21:01

Orion Adrian


People also ask

Where are Java exceptions stored?

The exception site list is managed in the Security tab of the Java Control Panel. The list is shown in the tab.

What do I put in an exception message?

The content of the exception message dependents on the receiver of the message - so you have to put yourself in that person's shoes. A support engineer will need to be able to identify the source of the error as quickly as possible. Include a short descriptive string plus any data that can help troubleshoot the issue.

Should exception messages end in a full stop?

Use grammatically correct error messages Write clear sentences and include ending punctuation. Each sentence in the string assigned to the Exception. Message property should end in a period.

What does exception message mean?

An exception occurs when a package or shipment encounters an unforeseen event, which could result in a change to the expected delivery day. Examples of exception include: address unknown, damage to shipment, or signature not received.


2 Answers

If your exceptions are strongly typed, you don't need to worry about messages. Messages are for presenting errors to users, and exceptions are for controlling flow in exceptional cases.

throw new InvalidOperationException("The Nacho Ordering system is not responding.");

could become

throw new SystemNotRespondingException("Nacho Ordering");

In the latter case, there's nothing to translate, and therefore no need to worry about localization.

like image 185
Michael Meadows Avatar answered Oct 23 '22 16:10

Michael Meadows


I may get shot (well, downvoted) for this, but why not "where you create the exception"?

throw new InvalidDataException("A wurble can't follow a flurble");

Unless you're going to internationalize the exception messages (which I suggest you don't) do you particularly need them to be constants etc? Where's the benefit?

like image 35
Jon Skeet Avatar answered Oct 23 '22 15:10

Jon Skeet