Cpen 221 fall 2016 exceptions 6 this suggests a more

Info icon This preview shows pages 5–7. Sign up to view the full content.

CPEN 221 – Fall 2016
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Exceptions 6 This suggests a more refined rule: • You should use an unchecked exception only to signal an unexpected failure (typ- ically, a bug), or if you expect that clients will usually write code that ensures the exception will not happen, because there is a convenient and inexpensive way to avoid the exception; • Otherwise you should use a checked exception. Here are some examples of applying this rule to hypothetical methods: Queue.pop() throws an unchecked EmptyQueueException when the queue is empty, because it’s reasonable to expect the caller to avoid this with a call like Queue.size() or Queue.isEmpty() . Url.getWebPage() throws a checked IOException when it can’t retrieve the web page, because it’s not easy for the caller to prevent this. • A new method we may want to write, int integerSquareRoot(int x) , might throw a checked NotPerfectSquareException when x has no integral square root, because testing whether x is a perfect square is just as hard as finding the actual square root, so it’s not reasonable to expect the caller to prevent it. The cost of using exceptions in Java is one reason that many Java API’s use the null reference as a special value. It’s not a terrible thing to do, so long as it’s done judiciously, and carefully specified. 7 Abuse of Exceptions Here’s an example from Effective Java by Joshua Bloch (Item 57 in the 2nd edition). try { int i = 0 ; while ( true ) a[i++]. f (); } catch (ArrayIndexOutOfBoundsException e) { } What does this code do? It is not at all obvious from inspection, and that’s reason enough not to use it . ... The infinite loop terminates by throwing, catching, and ignor- ing an ArrayIndexOutOfBoundsException when it attempts to access the first array element outside the bounds of the array. It is supposed to be equivalent to: CPEN 221 – Fall 2016
Image of page 6
Exceptions 7 for ( int i = 0 ; i < a. length ; i++) { a[i]. f (); } Or (using appropriate type T ) to: for (T x : a) { x. f (); } The exception-based idiom, Bloch writes: ... is a misguided attempt to improve performance based on the faulty rea- soning that, since the VM checks the bounds of array accesses, the nor- mal loop termination test ( i < a.length ) is redundant and should be avoided. However, because exceptions in Java are designed for use only under exceptional cir- cumstances, few, if any, JVM implementations attempt to optimize their performance. On a typical machine, the exception-based idiom runs 70 times slower than the stan- dard one when looping from 0 to 99. Much worse than that, the exception-based idiom is not even guaranteed to work! Sup- pose the computation of f() in the body of the loop contains a bug that results in an out-of-bounds access to some unrelated array. What happens? If a reasonable loop idiom were used, the bug would generate an uncaught exception, resulting in immediate thread termination with a full stack trace. If the misguided exception-based loop were used, the bug-related exception would be caught and misinterpreted as a normal loop termina- tion. CPEN 221 – Fall 2016
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.
  • Fall '17
  • satish

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern