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

Mock exam question on Exception

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am somewhat confused about what happens when all methods, including the main method, declare the exception of the risky method instead of handling it. There can be two scenarios from what I have understood -

Case (1) The code compiles, the risky method does not throw an exception and the program runs successfully

Case (2) The code compiles, the risky method does throw an exception and there's a run time error

Now consider Q21 from Practice Exam 1 in OCPJP Practice Exams by Kathy Sierra and Bert Bates -



"An exception is thrown at runtime" was one of the answer options but not the right answer. The solutions states that File I/O is risky but it's okay for main() to throw an Exception.

But what happens if an exception is actually thrown (Case 2)?? Will the program still run successfully?

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No - an Exception will be thrown.

But this is a checked Exception and not a Runtime-Exception (although it is catched at a higher level and forces the Program to stop).
 
k reeta
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for that. So for a question such as the one above do we just answer assuming that an exception will not be thrown?
 
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

k reeta wrote:Thanks for that. So for a question such as the one above do we just answer assuming that an exception will not be thrown?


Hi Reeta, If a method declares a 'Checked Exception' it is simply saying "Hey An Exception Might Be Thrown"...Hence You Cant Assume That An Exception Will NOT Be Thrown...It Is The Nature Of The Question That Will Determine That...What If Your Knowledge Is Being Tested On A Situation Where A FileNotFoundException Is Thrown?? Then In That Case You Cannot Assume That An Exception Will Not Be Thrown...It Is IMPORTANT To Read CAREFULLY And Understand The Question Being Asked...I Hope This Helps Thanks.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's great, when you discuss a question like this, to post the ENTIRE question. As has been mentioned in this thread, the questions on the real exam are worded extremely carefully. When we make mock questions we also try to word the questions extremely carefully. Often, the subtle language makes all the difference in getting the correct answer.
 
k reeta
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone for the tips. And point taken, Bert! So here's the complete question -


Given the following directory structure:



If test, testdir, subdir and subdir2 are all directories and Finder.class and testfile.txt are files, and given:



And, if the code compiles, the invocation:

java Finder testdir

What is the result?

A. 1
B. 2
C. 3
D. 4
E. 5
F. 100
G. Compilation fails
H. An exception is thrown at runtime

The answer says that C is correct and that File I/O stuff is risky but it's okay for main() to throw an exception.

My question is - how can we say with certainty that an exception won't be thrown at runtime, ruling out H.

Thanks.
 
Goodbye moon men. Hello tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic