Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

About Exception and Throwable

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am going to pass my certification in a few days...
I have a confusion over Exception and Throwable with the following code:

I am hesitating between E and F...Which one is the correct answer?
 
Ranch Hand
Posts: 52
Oracle Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will get the compilation error as you are throwing an error and catching an exception. See the hierarchy structure.
 
dishlad kumar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No compilation error...
I tried to run the code nad the result was:

i am a litte bit confused...
 
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

According to me, following answer is correct:

E.A Throwable is thrown by main

Java compiler only checks whether checked exceptions are caught or not. If method is throwing the checked exception and catch block doesn't specify that exception or superclass of it, then compilation error would occur. Here method is throwing error and compiler would not check if there exist a catch block for error or not. So there would not be any compilation error.

As error is subtype of Throwable , a Throwable is thrown by main.
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to The Ranch!

One source of the confusion may be that the message displayed by the JVM is a little misleading:
It produces the message "Exception in thread..." when any Throwable is handled by the JVM. Even if it's not actually an Exception object. Errors are in a different branch of the inheritance tree to Exception, but that message still gets shown. Think of it as meaning "exceptional condition" rather than "java.lang.Exception".

And yes, I'd agree: E. If an Exception was thrown it would be caught in the main method, and you'd get some output. But nowhere in the code is catching an Error.
 
dishlad kumar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys !!!
 
reply
    Bookmark Topic Watch Topic
  • New Topic