Almost all the exceptions I have ever written have been very lightweight, containing a String message and optionally a throwable. In some situations I have included some application specific enum or some other field.public class MySpecialException(){    private MyErrorCode errorCode;    public MySpecialException(String message, Throwable cause, MyErrorCode errorCode)    {        super(message, cause);        this.errorCode = errorCode;    }    ....}Now I face a situation where I need to put in 5 or 6 fields in the exception because the error handler that catches them needs them to generate the output.Would you consider that to be bad code? Can an exception be too big?public class MySpecialException(){    private String name;    private int age;    private int id;    private int height;    private String duck;    private String whatever;    ....}
How much data should exceptions hold?
design;exceptions;exception handling
Of course it can be too big - virtually everything about coding is a trade-off between different principles, and not wasting memory is one of them.However, if you need the data in the exception to get the job done, then by definition it's not too big. It would be too big only if there was another mechanism that achieves the same, leads to maintainable code and works like an exception.But exceptions are first-class language elements that were invented specifically to achieve something that you can't otherwise do (break control flow and transport data somewhere else in the call stack without having to change the intermediate callers), so I doubt that you could find another solution which fulfills that condition.