• 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

 
Greenhorn
Posts: 12
  • 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[])
{
System.out.println("Before Try");
try
{
}
catch(java.io.IOException t)
{
System.out.println("Inside Catch");
}
System.out.println("At the End");
}
}
Why this give error? But the following code does not give error
public class AQuestion
{
public static void main(String args[])
{
System.out.println("Before Try");
try
{
}
catch(Exception t)
{
System.out.println("Inside Catch");
}
System.out.println("At the End");
}
}
Thanks
In advance
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
b bajwa

A couple of points. If your asking about whay you get an error you should include the error you are getting in your post and whether it is a run time or compile time error. Some people can't compile and run the code in the posts so the more info the better chance of them figuring out what the problem is.
That being said, I assume your error is something about the first set of code not throwing an IOException in the block. This error occurs because you're trying to catch an Exception that can not occur the way the code is written. The second set of code will compile fine becuase you're catching EXception, this includes any type of Exception including RunTimeExceptions which can occur anywhere at any time, so there is no problem.
Also, your name is not in keeping with our naming policy here at the ranch. Please re-register under an appropriate name as shown in the policy.
Thanks again and we hope to see you around the ranch!!


------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would say its because IOException is a checked exception, and at no point does the try block attempt to throw one. Exception can catch unchecked exceptions like a divide by 0 which do not need to be declared to be thrown?
is that right?
 
b bajwa
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks dave and stephen
 
Bras cause cancer. And tiny ads:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic