SLIDE 16 Error Handling Input-Output Writing Better Code Errors Defensive programming Exception Handling Assertions
Throwing an exception
✞
/∗∗ ∗ Look up a name or phone number and return the ∗ corresponding contact d e t a i l s . ∗ @param key The name or number to be looked up . ∗ @return The d e t a i l s corresponding to the key , ∗
n u l l i f there are none matching . ∗ @throws NullPointerException i f the key i s n u l l . ∗/ public ContactDetails getDetails ( String key ) { i f ( key == null ){ throw new NullPointerException ( ” n u l l key in getDetails ” ) ; } return ( ContactDetails ) book . get ( key ) ; }
✡ ✝ ✆
Lecture D.10. (MDV) Programming I Academic Year 2012-2013 16 / 77