180 likes | 204 Views
Practical Exception Specifications. ECOOP Workshop Exception Handling in Object Oriented Systems Donna Malayeri and Jonathan Aldrich. Exceptions: a double-edged sword?. Useful for writing error-handling code allows localizing handler code harder to ignore error conditions
E N D
Practical Exception Specifications ECOOP Workshop Exception Handling in Object Oriented Systems Donna Malayeri and Jonathan Aldrich
Exceptions: a double-edged sword? • Useful for writing error-handling code • allows localizing handler code • harder to ignore error conditions • But can produce confusing control flow • Have been compared to GOTO
Are tools the answer? • Most require whole-program analysis, and are not scalable • Don’t allow specifying and enforcing a policy • bad for team development
Are exception specifications the answer? • Not current specification systems: • too heavyweight • too fine-grain • difficult to maintain • little or no help for understanding exception flow
Error: unhandled exception SQLException setValue(Object val) setValue(key, val) theDB.save(key, value);
Error: unhandled exception SQLException Error: unhandled exception SQLException setKey(String key, Object val) setValue(Object val) throws SQLException setValue(key, val) throws SQLException theDB.save(key, value);
publicsetKey(String key, Object val) throws SQLException throws SQLException Problem! Programmer didn’t realize that UserPrefs is supposed to hide storage details setValue(Object val) throws SQLException User Preferences abtraction is violated! setValue(key, val) throws SQLException theDB.save(key, value);
So, Java “throws” is not a solution • Not expressive • Not lightweight • Difficult to maintain • Easily become imprecise • No aid in comprehension of exception flow
In contrast, practical exception specifications should be: • Expressive • Lightweight • Easy to maintain • An aid for understanding exception flow and exception policies • ExnJava has all of these properties
ExnJava is more expressive Specify exception policies for Java packages Programmer adds specification: package util.userPrefs may only throw PreferenceException
ExnJava combines inference and annotations • Programmer writes throws declarations for public and protected methods only • Other declarations are inferred • Performs a per-package analysis • scalable • future work: modules instead of packages
ExnJava Error: Public methods in util.userPrefs may not throw SQLException inferred inferred Error: unhandled exception SQLException User Preferences Module • public{PrefKeys.getKey(key).setValue(val);} • publicsave() {Serializer.save();} class UserPrefs setKey(String key, Object val) • {Serializer.setValue(this, val);} class PrefKeys setValue(Object val) throws SQLException • static{cache.store(key, val);} class Serializer throws SQLException setValue(key, val) theDB.save(key, value);
ExnJava User Preferences Module class UserPrefs • publicsetKey(String key, Object val){try{PrefKeys.getKey(key).setValue(val);} catch (SQLException e) {throw new PreferenceException(e); } } • publicsave() {Serializer.save();}
ExnJava specifications are maintainable • Fewer declarations means • fewer declarations to maintain • fewer declarations that can become imprecise • ExnJava includes tools • fix imprecise declarations • propagate throws declarations
Related Work • Robillard & Murphy • Jex • Sinha et al • exception analysis • views for exception flow
Summary of ExnJava Properties • Lightweight annotations • combination of inference and user annotations • Expressive • supports package exception policies • Easy to maintain • fewer annotations • tools: Propagate Throws, Fix Imprecise • Aids understanding exception flow • detailed views