Skip to main content
How to Discard Objects That Throw Catch-All Exceptions in java ?
Discard Objects That Throw Catch-All Exceptions
In many cases, these exceptions indicate that the internal state of the invoked object is corrupt, and that further invocations will also fail. Keep the object reference only if careful scrutiny of the exception shows it is benign, and further invocations on this object are likely to succeed.
Adopt a guilty unless proven innocent approach. For example, a SQLException
thrown from an Oracle JDBC invocation could represent one of thousands of error conditions in the JDBC driver, the network, or the database server. Some of these errors (for example, subclass SQLWarning
) are benign.
Some SQLExceptions
(for example, "ORA-3113: end of file on communication channel") definitely leave the JDBC object useless. Most SQLExceptions
do not clearly specify what state the JDBC object is left in.
The best approach is to enumerate the benign error codes that could occur frequently in your application and can definitely be retried, such as a unique key violation for user-supplied input data. If any other error code is found, discard the potentially corrupt object that threw the exception.
Discard all object references to the (potentially) corrupt object. Be sure to remove the corrupt object from all pools in order to prevent pools from being poisoned by corrupt objects. Do not invoke the corrupt object again - instantiate a brandnew object instead.
When you are sure that the corrupt objects have been discarded and that the catching object is not corrupt, throw a non catch-all exception so that the caller does not discard this object
Comments
Post a Comment