This week's giveaway is in the Testing forum.
We're giving away four copies of TDD for a Shopping Website LiveProject and have Steven Solomon on-line!
See this thread for details.
Win a copy of TDD for a Shopping Website LiveProject this week in the Testing 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
  • 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

Another Question

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class AQuestion
{
public static void main(String args[])
{
try
{
System.out.println("Hi");
}
catch ( Exception ioe)
{

}
}
}

In the above program if i catch a IOException instead of Exception, a compile time error is given. Whereas, if an Exception is caught, no error arises. I would appreciate if some one can explain me why ?
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An IOException is a checked exception, and if there are no statements in the try block that might cause an IOException, then you can have a single catch block to catch IOException.

On the other hand, RuntimeException is a subclass of Exception, and so it's possiblet that an unchecked exception might be thrown in the try block.
 
Srinu Nanduri
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow Keith,

Wonderful Answer, I will never forget this in future. Perfect. Thanks a lot for your reply.

Hope this continues until i get the SCJP.

And one more question i have regarding the preparation for SCJP.
I have read the books like Head First Java, Complete Reference, Thinking in Java etc and for certification books like SCJP by Heller and Roberts, and partly the book by Khalid Mughal and Rolf.

Still i am having questions of this sort. Is it possible or do i need to read more books ? Please suggest me to over come the problem and do well in the exam.

Thanks.

Your response will value a lot for me.

Srinu
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry!

I have some doubt hear!

Do we need to have a code that can throw an exception, and then only we can have a catch block which catches that type of exception?
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the only catch block you have catches an exception which is checked (and not Exception), then there must be a line of code in the try block that can cause that checked exception to occur.
 
If you like strawberry rhubarb pie, try blueberry rhubarb (bluebarb) pie. And try this tiny ad:
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