• 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

Exception Doubt..

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question 1
----------

If I write a code like the one below why does it not give a compilation error. If I change Exception to IOException it does throw a compilation error.





Question 2
----------

If an IOException is thrown, then even an Exception can catch it because it is its super class. Then why not an Object.

 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The exceptions that are subclasses of RuntimeException are unchecked. So by catching an Exception you are looking for a wide range of exceptions. But if you choose a specific checked exception, then you need to make sure there is some code in the try block that has the potential for throwing that exception.

The parameter in the catch block must be a subclass of Throwable.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi KrishnaKumar.

Further to what Keith said,
I think question one also involves "implicit import" of java.lang.* library. I read somewhere that this happens, but I dunno much about it.

Note that Exception and other exceptions such as SecurityException, ArithmeticException are in the library and do not need any additional import declaration. See API.

If you want to use IOException, you have to import java.io and you also have to put some code in try block that will possibly throw IOException eg

FileReader fr = new FileReader("someFileName.txt");

As for Question 2, only Throwable and subclass can be caught (Keith) and while an exception is an object an object is not an exception.
[ March 29, 2005: Message edited by: Jeff Tian ]
 
30 seconds to difuse a loaf of bread ... here, use this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic