• 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 ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

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

 
Ranch Hand
Posts: 118
1
  • 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).
 
author & internet detective
Posts: 41763
887
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code does compile. This is the code on page 853. Notice how the main method has "throws Exception". That takes care of all the exceptions you described. The TimeoutException is caught in particular because the example is highlighting how timeouts work.

 
Nyeng Gyang
Ranch Hand
Posts: 118
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I missed the fact that the main() method actually throws Exception. Thanks for pointing this fact, which I missed.
 
Those who dance are thought mad by those who hear not the music. This tiny ad plays the bagpipes:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic