Practical Exception Specifications ECOOP Workshop Exception Handling in

  • Slides: 18
Download presentation
Practical Exception Specifications ECOOP Workshop Exception Handling in Object Oriented Systems Donna Malayeri and

Practical Exception Specifications ECOOP Workshop Exception Handling in Object Oriented Systems Donna Malayeri and Jonathan Aldrich 25 Jul 2005 1

Exceptions: a double-edged sword? o Useful for writing error-handling code n n o o

Exceptions: a double-edged sword? o Useful for writing error-handling code n n o o allows localizing handler code harder to ignore error conditions But can produce confusing control flow Have been compared to GOTO 25 Jul 2005 2

Are tools the answer? o o Most require whole-program analysis, and are not scalable

Are tools the answer? o o Most require whole-program analysis, and are not scalable Don’t allow specifying and enforcing a policy n bad for team development 25 Jul 2005 3

Are exception specifications the answer? o Not current specification systems: n n too heavyweight

Are exception specifications the answer? o Not current specification systems: n n too heavyweight too fine-grain difficult to maintain little or no help for understanding exception flow 25 Jul 2005 4

Example: Java throws declarations User Preferences Module class User. Prefs public set. Key(String key,

Example: Java throws declarations User Preferences Module class User. Prefs public set. Key(String key, Object val) { Pref. Keys. get. Key(key). set. Value(val); } public save() { Serializer. save(); } class Pref. Keys set. Value(Object val) { Serializer. set. Value(this, val); } Error: unhandled exception SQLException class Serializer static set. Value(key, val) { the. DB. save(key, cache. store(key, value); val); } 25 Jul 2005 5

Example: Java throws declarations User Preferences Module Error: unhandled exception SQLException class User. Prefs

Example: Java throws declarations User Preferences Module Error: unhandled exception SQLException class User. Prefs public set. Key(String key, Object val) { Pref. Keys. get. Key(key). set. Value(val); } public save() { Serializer. save(); } class Pref. Keys Error: unhandled exception SQLException set. Value(Object val) throws SQLException { Serializer. set. Value(this, val); } class Serializer static set. Value(key, val) throws SQLException { the. DB. save(key, cache. store(key, value); val); } 25 Jul 2005 6

Example: Java throws declarations User Preferences Module class User. Prefs public key, Object val)

Example: Java throws declarations User Preferences Module class User. Prefs public key, Object val) throws publicset. Key(String Object val) SQLException { Pref. Keys. get. Key(key). set. Value(val); } public save() Problem! { Serializer. save(); } Programmer didn’t realize that User. Prefs is. Pref. Keys supposed to hide storage details class set. Value(Object val) throws SQLException User Preferences abtraction { Serializer. set. Value(this, val); } is violated! class Serializer static set. Value(key, val) throws SQLException { the. DB. save(key, cache. store(key, value); val); } 25 Jul 2005 7

So, Java “throws” is not a solution o o o Not expressive Not lightweight

So, Java “throws” is not a solution o o o Not expressive Not lightweight Difficult to maintain Easily become imprecise No aid in comprehension of exception flow 25 Jul 2005 8

In contrast, practical exception specifications should be: o o o Expressive Lightweight Easy to

In contrast, practical exception specifications should be: o o o Expressive Lightweight Easy to maintain An aid for understanding exception flow and exception policies Exn. Java has all of these properties 25 Jul 2005 9

Exn. Java is more expressive Specify exception policies for Java packages Programmer adds specification:

Exn. Java is more expressive Specify exception policies for Java packages Programmer adds specification: package util. user. Prefs may only throw Preference. Exception 25 Jul 2005 10

Exn. Java combines inference and annotations o o o Programmer writes throws declarations for

Exn. Java combines inference and annotations o o o Programmer writes throws declarations for public and protected methods only Other declarations are inferred Performs a per-package analysis n n scalable future work: modules instead of packages 25 Jul 2005 11

Exn. Java User Preferences Module Error: unhandled exception SQLException class User. Prefs public set.

Exn. Java User Preferences Module Error: unhandled exception SQLException class User. Prefs public set. Key(String key, Object val) { Pref. Keys. get. Key(key). set. Value(val); } public save() Error: { Serializer. save(); } Public methods in util. user. Prefs may not class Pref. Keys inferred throw SQLException set. Value(Object val) throws SQLException { Serializer. set. Value(this, val); } class Serializer inferred static set. Value(key, val) throws SQLException { the. DB. save(key, cache. store(key, value); val); } 25 Jul 2005 12

Exn. Java User Preferences Module class User. Prefs public set. Key(String key, Object val)

Exn. Java User Preferences Module class User. Prefs public set. Key(String key, Object val) { try { Pref. Keys. get. Key(key). set. Value(val); } catch (SQLException e) { throw new Preference. Exception(e); } } public save() { Serializer. save(); } 25 Jul 2005 13

Exn. Java specifications are maintainable o Fewer declarations means n n o fewer declarations

Exn. Java specifications are maintainable o Fewer declarations means n n o fewer declarations to maintain fewer declarations that can become imprecise Exn. Java includes tools n n fix imprecise declarations propagate throws declarations 25 Jul 2005 14

Demo 25 Jul 2005 15

Demo 25 Jul 2005 15

Related Work o Robillard & Murphy n o Jex Sinha et al n n

Related Work o Robillard & Murphy n o Jex Sinha et al n n exception analysis views for exception flow 25 Jul 2005 16

Summary of Exn. Java Properties o Lightweight annotations n o Expressive n o supports

Summary of Exn. Java Properties o Lightweight annotations n o Expressive n o supports package exception policies Easy to maintain n n o combination of inference and user annotations fewer annotations tools: Propagate Throws, Fix Imprecise Aids understanding exception flow n detailed views 25 Jul 2005 17

25 Jul 2005 18

25 Jul 2005 18