Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Whizlabs Exception Handling Question

 
J Sato
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a concern regarding the following from the Whizlabs quiz:

Consider that you have separate catch clauses for handling Throwable, Exception, and RuntimeException. Which catch clause has to appear first for the code to compile correctly?

A catch(Exception e){}
B catch(Throwable t){}
C catch(RuntimeException re){}

I chose A but Whizlabs said C was the correct answer. I confirmed C is correct with the following code:



I understand why C is correct because RuntimeException is narrower than Exception but from reading the K & B book (page 361) I understood that RuntimeExceptions are unchecked (like errors) so it did not matter if they were handled or declared.

Just for my own clarification, if Errors or RuntimeExceptions are caught, they must also adhere to the "narrowest first" rule, right? If this is in the book and I missed it, I'd greatly appreciate it if someone could let me know where it's discussed.

Thanks.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general you do not have to deal with unchecked exceptions. However if you do have a catch block for an unchecked exception, it has to follow the same rules that apply to any catch block.
 
J Sato
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the confirmation!
 
debolina chakravarty
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All!

While going through some of the posts of people who have passed the SCJP 1.4 exam, I found that some of them have got questions from files and Swing. Can you please advice me what I should study? I am taking the exam in 2 days so if you could tell me what all is relevant to the exam(as these are not there in the exam objectives), I would be very grateful.

Thanks.
 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Debolina,

While going through some of the posts of people who have passed the SCJP 1.4 exam, I found that some of them have got questions from files and Swing. Can you please advice me what I should study?

File and Swing topics are not in the SCJP 1.4 exam. Also, have a look at this at SCJP FAQ.

Joyce
 
debolina chakravarty
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats a relief!!
Thanks Joyce!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic