• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

OCA Java SE 8 Programmer I mistake page 305 Runtime exception Okay for program to catch?

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OCA Java SE 8 Programmer I mistake page 305 Runtime exception Okay for program to catch?  Answer should be NO, but instead it is Yes. By alinvlad05.
 
Saloon Keeper
Posts: 1603
51
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Firstly, welcome to the Ranch!

Not everyone has that version of that book.

Eventually the authors will come around and see this, but it would be both good and permissible to post the whole question here, because of the arrangement we have with the authors.

Originally, the notion was that it is required to catch checked exception types and usually a mistake to catch unchecked (RuntimeException) ones.

But the Java community has moved away from that original simple notion, it is reasonably common to need to catch and handle certain sub-classes of RuntimeException, particularly when parsing input coming in as String types that may contain a format error.

Post the whole question and answer.

Lastly, always check the errata page for the book found at:

https://www.selikoff.net/java-oca-8-programmer-i-study-guide/

Tho I don't see this one mentioned there, unless I missed it.  Mistakes in answers are listed under the page for the answer, rather than the page containing the question.
 
Vlad Alin
Ranch Hand
Posts: 35
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you.
 
author & internet detective
Posts: 40791
828
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
Why do you think it's not ok for a program to catch a runtime exception?

As an example, I often write programs that call REST APIs. Sometimes I catch the network exception thrown to capture info about the failure or do a retry.
 
Vlad Alin
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In OCA java 8 book Chapter 6 Exceptions Question 19 explanation.
"C, E. Option C is allowed because it is a more specific type than RuntimeException.
Option E is allowed because it isn’t in the same inheritance tree as RuntimeException. It’s not a good idea to catch either of these."  

From this explanation i thought it's not okay to catch RuntimeException.
 
Vlad Alin
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
C is IllegalArgumentException which is a RuntimeException
 
Vlad Alin
Ranch Hand
Posts: 35
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe the expalation refers specifically to IllegalArgumentException.
 
Jeanne Boyarsky
author & internet detective
Posts: 40791
828
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
There's a subtle distinction here. It's not recommended, in general, to catch a RuntimeException. However, it is allowed if you have a specific need like in the network call example I gave.

Whereas you catching Error and Throwable is legal but should never be done.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic