• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

A so similar Question,but a different result,Why?(quiz-12)

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pls read the code following,the code1 are almost the same except the type of the Exception in catch is different .but the code1 compile and run,while the code2 give a comile error.
What's the wrong with the code2?
//------------------code1------------------------//
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");
}
}

//----------------------code2-----------------------//
public class AQuestion
{
public static void main(String args[])
{
System.out.println("Before Try");
try
{
}
catch(java.io.IOException t)
{
System.out.println("Inside Catch");
}
System.out.println("At the End");
}
}
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Throwable is a parent class of all exceptions/errors including runtime exceptions, while IOException is a specific checked exception. The compier checks if the code in try{..} block for IOException can throw an IOException, Whereas unchecked runtime exception (for Throwable) etc, can occur anytime while the code is running, hence no such check is done when you catch Throwable (this will be true even for Exception).
So code1 compiles and code2 doesn't.
HTH,
- Manish
 
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Manish Hatwalne:
Throwable is a parent class of all exceptions/errors including runtime exceptions, while IOException is a specific checked exception. The compier checks if the code in try{..} block for IOException can throw an IOException, Whereas unchecked runtime exception (for Throwable) etc, can occur anytime while the code is running, hence no such check is done when you catch Throwable (this will be true even for Exception).
So code1 compiles and code2 doesn't.
HTH,
- Manish


So when we try{empty} catch{uncheck_exceptions} ,any unchecked
exceptions it will complie? But with checked exceptions some code
has to be throwable on that exceptions u trying to catch in
the try{} ?
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Throwable is the parent of all Exceptions and it is not a checked exceptions. It does not require you do something inside the try{} to throw itself.
But IOExecption is checkedexception and that requires you to do something which can throw an exception which is IOException or its subclass

Goodluck
ragu
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic