I have a custom exception class which includes an optional error code, and I'd like to include this error code in the Exception's toString() output. I assume that this is just a matter of appending the error code to the result of super.toString(), something like this:
It would be more idiomatic to supply the error code as part of getMessage(); include the code in your call to super() in your class's constructor. Exception.toString() isn't going to be called often in production code.
So when a stack trace is printed the exception's getMessage() is called, instead of toString()? If so then it seems that what I really want to do is to override getMessage() so that the String it returns will include the error code if it is set.
Can you elaborate on what you mean about supplying the error code as part of getMessage() by including the code in the call to super() in the constructor? I already call super(message) in my constructor which takes a message argument, and there's no super() method which takes an integer argument, and besides the error code isn't known at creation time. Obviously I'm confused as to what you're suggesting.