• Post Reply Bookmark Topic Watch Topic
  • New Topic

Custom Exception with many parameters  RSS feed

 
maddy snow
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two way to write my exceptions:

public class MyException extends Exception{
public MyException(String msg){
super(msg);
}
}

Or:
public class MyException extends Exception(){
public MyException(MyObject1 myObject1, MyObject2 myObject2){
super(myObject1.toString() + myObject2.toString());
}
}

Which one is better? Is it a good idea to provide many details in an exception?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maddy snow wrote:Which one is better?

The one that contains enough information to make it useful and no superfluous information.
Without any context it's impossible to give you any more than that.
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's wrong with the option of allowing both?

If your answer is that you don't want arbitrary strings passed to the constructor, but the string must be built from two object instances, then the right way is to have your code implement that "must" rather than passing the responsibility of doing that to every single class which needs to use the exception.

Also if you choose the second version (only), then it may be useful for the exception object to return references to the two objects which were used to create it. That's just speculation of course, because we know nothing about your custom exception and what it's meant to do.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!