Women in Technology

Hear us Roar



Article:
  Best Practices for Exception Handling
Subject:   Modification proposed to java exception handling
Date:   2003-12-05 14:26:20
From:   anonymous2
IMHO,
The java checked exception mechanism could be enhanced by having a :


catchlog(Exception e)


construct in addition to


catch(Exception e)


the proposed catchlog construct would by default log the exception stacktrace and no body is needed. On the other hand the catch construct could be made more robust by adding a compile time check to see that the catch body is not empty.


Example:


[case a]: "I don't want the interference of the checked
exception "ServiceException" thrown by method "doSomething()" of class "ServiceProvider" in my code"
//-- code 1 --//
try {
serviceProvider.doSomething();
} catchlog(ServiceException se); //note this requires no body
//-- end code 1 --//



[case b]: "I want to do some error recovery"


//-- code 2 --//
try {
serviceProvider.doSomething();
} catch(ServiceException se) { // do some error recovery
doErrorRecovery(se);
}
//-- end code 2 --//


The advantage of this is that if I leave the catch block in case b empty and continue to use the "catch" construct instead of "catchlog", the compiler will complain.



I understand that by enforcing this simple
change in newer versions of Java some of the existing code may break (when there exist empty catch block).
One suggestion for incorporating this in the newer JDK version without breaking the codes written for previous versions is that the compiler signal a warning instead of error in case the above specified contract is violated.


~ Amol Deshmukh [deshmua@ececs.REMOVETHIS.uc.edu]
Graduate Student,
Electrical and Computer Engineering and Computer Science Dept.,
University of Cincinnati