• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • Frits Walraven
  • Himai Minh

Too Restrictive Exception Handling in the OCP Java SE 11 Developer Complete Study Guide

Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have just noticed that the code presented on the bottom half of page 853 of the study guide does not compile because of the too restrictive checked exception class caught in the catch block. The two-parameter get() method of the interface Future throws exceptions of the following checked exception classes: ExecutionException, InterruptedException and TimeoutException. I have discovered that catching only one of these exception produces a compile-time error, which reports that one other exception has not been caught or declared, as follows:

(1.) Catching only TimeoutException results in the error notice that InterruptedException must be caught or declared.
(2.) Catching only InterruptedException results in the error notice that ExecutionException must be caught or declared.
(3.) Catching only ExecutionException results in the error notice that InterruptedException must be caught or declared.

Since all the foregoing three checked exception classes extend the class Exception, the only resolution, to the vicious cycle of catching only one exception and having the corresponding compile-time error reported, is either to catch exceptions of class Exception in one catch block or use multiple catch blocks to catch each of the three exceptions in its own separate catch block.

Anyways, I just thought to note here that the code, as presented on page 853, does not compile (because the too restrictive/specific TimeoutException, rather than the more general Exception, was caught in the catch block).
Get out of my mind! Look! A tiny ad!
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic