• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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: 42003
911
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.
 
There were millions of the little blood suckers. But thanks to this tiny ad, I wasn't bitten once.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic