J
John W. Kennedy
Lew said:There are perfectly valid engineering reasons for wrapping exceptions
and rethrowing them as custom exceptions, or eating the exception and
converting to a non-exceptional value. It has to do with calling classes
not caring any more what the original exception is, if it was logged at
the lowest point, but only that an application-level exception occurred,
or no exception if the called method ate it.
Another reason to rethrow just came up for me yesterday, involving
parsing a complex CSV file.
In parse-field routine, throw MyFieldSyntaxException
In parse-record routine, catch MyFieldSyntaxException and
throw MyRecordSyntaxException, identifying the field
being processed at the time.
In read-file routine, catch MyRecordSyntaxException and
throw MyFileSyntaxException, identifying the record
being processed at the time.
In main method, catch MyFileSyntaxException, and give
the user all the bubbled-up information to diagnose the
problem.
The alternate approach would be to pass "where am I?" information all
the way down to the "parse-field" routine, information that that routine
has no use for unless it needs to construct the exception -- and that
scheme immediately breaks down if, say, the parse-field routine must
later be applied to a VARCHAR column of an SQL database.