Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

shall we put rich information in customized exception ?

 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose I have a customized exception for my project --

public class CustomerException extends Exception {
public Customerxception() {}

public CustomerException(String msg) {
super(msg);
}
}

In my code, whatever checked standard exception I catch, I do

throw new CustomerException(" some messge here...");

to throw to client side a "CustomerException". Fairly simple. The question is -- Shall I put "rich" information in the argument of "CustomerException" ? i.e shall I do things like

throw new CustomerException("Backend Error"); // short version msg

or

throw new CustomerException("There is a backend database error happended when table" + table + "is being updated by client " + client);

How do you guys usually do ?

The CustomerException is thrown to my client side code.

Thanks.
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As usual, it depends. Show the user as much information as they need to know. But for logging I tend to get as detailed as I can without going overboard.
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Certainly a preference (and possibly desired to be localized), but I like to be detailed and offer a way around the problem or a possible cause.

/snark-asm/
Or, if you're done acquiring Yahoo, you could politely throw an exception 41850, which could easily be cross referenced in the user handbook.
 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then after the client side catches "CustomerException", is it going to really check the e.getMessage() to see what's the message ? I thought usually when client gets the "CustomerException" it just does something mechanically. If you put rich and detailed information into the Exception constructor, does it mean you want client to check this messge and do different things according to the different messages ? If that;s the case, are you going to hard-coded the client code like

if (e.getMessage().equals("This is a database error")
// go to databse error page
if(e.getMessage().equals("This is a input error")
// go to input error page
...

This sounds crazy.. Maybe my understanding is wrong. Please tell me how client can really use the "messge" embedded in the exception constructor.

Thanks.
 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ben oliver:

if (e.getMessage().equals("This is a database error")
// go to databse error page
if(e.getMessage().equals("This is a input error")
// go to input error page
...

This sounds crazy.. Maybe my understanding is wrong. Please tell me how client can really use the "messge" embedded in the exception constructor.

Thanks.
If that sort of thing happens, you are better with CustomerDatabaseException, CustomerInputException, etc, etc
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has been my experience that most people do not want all the information I tend to give. I suggest putting up an explaination on the console that most people could interpret as something wint arrrohng.

Put things like jump table indexes or technical trace-log messages in a log file so the tech that is trying to fix it can inform supervisor where the problem is. Don't let exception catching become bulky, but put something there that can be used to trace the matter.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic