I have a use case where I want to return some data (an object) as I throw an exception. I'm considering storing the data in my custom checked exception class and accessing it via a getter when the exception is caught higher up in the stack. Is this considered a bad idea? If so what are some better alternatives? I've seen sending relevant messages to the customized exception but haven't really seen it being used as a data store.
I did stumble across Return a value AND throw an exception? and one of the answers there is doing something similar. I was thinking more along the lines of overloading the constructor and supplying the object instead of passing it as another argument. Is that considered bad coding practice?
public class NameException extends Exception
{
private static final long serialVersionUID = -4983060448714460116L;
private Service externalService;
public NameException(final String message)
{
super(message);
}
public NameException()
{
}
public NameException(Service externalService)
{
this.externalService =externalService;
}
public Service getExternalService()
{
return externalService;
}
}