Suppose I have a MyException class that is a subclass exception. I am using this class when my code contains relevant information when errors occur.
I usually use it to wrap one of the "standard" exception classes. For example, if an error occurs during the input validation, then I will do something like
if new (invalidInput ()) new MyException (new invalid record exception), arg1, arg2, ...) throw;
But my IDE (Intelligence IDEA) has warned me that it is bad despite hurting instantly without any unwanted exceptions (illegal exception in this example), but why do not I tell .
So how is it so sinful to make an exception without throwing it? In which cycle of hell will I go?
You can throw an instance of an exception in an illegal way. This is the case that is what it is for:
If invalidInput ()) new invalid record exception ("invalid argument" + + "+ expected ...");
Or otherwise expand, exception exception instead of invalid exception, if you want to increase it with custom properties.
Public category MyIllegalArgumentException provides an invalid extension exception {public MyIllegalArgumentException (Object AGR ...) {....}}
In both cases A lean, more meaningful, class model is available.
Update: Commented about the desire to provide relevant information with the thrown exception - you can do this by supplying your custom exception object that you can do the standard exceptions. Flip Throwreak Logic like this so that: Exceptions in exceptions instead of wrapping the relevant standard, you wrap the exception in relevant standard exception .
If invalid (InvalidInput ()) new invalid illegal exception ("invalid argument" + x + ", expected ..", new MyContextException (A, B, C));
(Where are the different bits of A, B & C references that you want to transmit). This way you (re) is a meaningful & amp; Appropriate, exception, at all points in the code, but you transmit the relevant information that you may want to use the stack more during settling / longing the exception.
No comments:
Post a Comment