• 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
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

SCJP / exception handling

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello all,
in doing a mock exam i came across this code

public class AQuestion
{
public static void main(String args[])
{
System.out.println("Before Try");
try
{
}
catch(Throwable t)
{
System.out.println("Inside Catch");
}
System.out.println("At the End");
}
}



1. Compiler error complaining about the catch block, where no Throwable object can ever be thrown.
2. Compiler error - Throwable Object can not be caught, only Exceptions must be caught.
3. No compiler error. The lines "Before Try" and "At the end" are printed on the screen.

i would have answered 1, but answer is 3. any reason of why compiler doe snot complain that exception will never be raised?
if i replace Throwable with java.io.IOException the compiler complaints...

any hints?

thanks and regards
marco
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not very sure but to my understanding the reason here is that the throwable class have the subclass exception and error which can occur anytime and cannot be declared that why the compiler doesnot complain in the following code.

Somebody has to verify I am right or wrong
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is because java compiler checkes only for the catch blocks which are catching checked exceptions. If you are catching any of checked exceptions which is not thrown by the code in try block, compiler will give error. In this case Throwabe do not come under checked exceptions but IOException do. Hence option 3 is correct.
 
I didn't do it. You can't prove it. Nobody saw me. The sheep are lying! This tiny ad is my witness!
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic