Ok, I'm a little confused about something...
I get the idea of Exceptions and the 'Inner Exception' or "innerEx", if you will. But the more I think about it, the more that I have trouble seeing any use for the 'innerEx' parameter for an exception class's constructor.
For example, I have my own custom Exception classes, and I have dutifully provided an 'innerEx' parameter for all my constructors. But I think I have never, ever used them when throwing an exception. The reason is that either I know what went wrong, in which case I throw the correct error type without any 'innerEx' provided, or I don't know, in which case I just re-throw via a 'Throw' call without any parameters in order to preserve the error stack trace when eventually handled by the higher-level error handler.
As an example, the System.ArgumentOutOfRangeException has a constructor overload that provides for a 'message' and an 'innerException'. Now, if the argument passed in is out of range, how can there be an inner exception at all? If the method is pre-checking the values and finds the argument out of range, then there can be no inner exception...
... I suppose that one could do post-validation, that is, don't pre-check, but surround the code with a Try-Catch-Finally block, and then only check for invalid arguments or the like within the Catch block. This is fine, and in this case there could theoretically be an inner exception to report. But would it make any sense to actually report that 'innerEx' here? For example, if the caller passed in, say, a null reference, or perhaps a negative integer where only positive numbers are valid, or some other invalid argument, then shouldn't the error itself be reported only? What purpose could it serve to report the error and in addition report the chaos this invalid argument caused when executed??
I was wondering what you all thought about this because I think I'm leaning towards removing the 'innerEx' parameters from the constructors to my exception classes. Certainly for the Argument-related exceptions.
Any thoughts?
I get the idea of Exceptions and the 'Inner Exception' or "innerEx", if you will. But the more I think about it, the more that I have trouble seeing any use for the 'innerEx' parameter for an exception class's constructor.
For example, I have my own custom Exception classes, and I have dutifully provided an 'innerEx' parameter for all my constructors. But I think I have never, ever used them when throwing an exception. The reason is that either I know what went wrong, in which case I throw the correct error type without any 'innerEx' provided, or I don't know, in which case I just re-throw via a 'Throw' call without any parameters in order to preserve the error stack trace when eventually handled by the higher-level error handler.
As an example, the System.ArgumentOutOfRangeException has a constructor overload that provides for a 'message' and an 'innerException'. Now, if the argument passed in is out of range, how can there be an inner exception at all? If the method is pre-checking the values and finds the argument out of range, then there can be no inner exception...
... I suppose that one could do post-validation, that is, don't pre-check, but surround the code with a Try-Catch-Finally block, and then only check for invalid arguments or the like within the Catch block. This is fine, and in this case there could theoretically be an inner exception to report. But would it make any sense to actually report that 'innerEx' here? For example, if the caller passed in, say, a null reference, or perhaps a negative integer where only positive numbers are valid, or some other invalid argument, then shouldn't the error itself be reported only? What purpose could it serve to report the error and in addition report the chaos this invalid argument caused when executed??
I was wondering what you all thought about this because I think I'm leaning towards removing the 'innerEx' parameters from the constructors to my exception classes. Certainly for the Argument-related exceptions.
Any thoughts?
Last edited: