• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

IOException / Exception

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All
I M Still Not Clear About The Concept.Its OK That Complier wont give Error in Case of Runtime Exceptions as we already Know.
But Why it is specifically giving error on IOException.Even with Exception It works Perfectly. e.g
class Temp
{
public static void main(String args[])
{
try{}
catch(Exception e)
}
} //Compiles and Run
class Temp
{
public static void main(String args[])
{
try{}
catch(IOException e)
}
} //Compile Time Error

Please Explain Why So..
Thanks

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Naveen ,
It is because IOException is not in java.lang package . It is in java.io package .
Include this statement in your program and it will run fine :
import java.io.IOException;
Rgds
 
Naveen Sharma
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry I Forgot write to import java.io.*;
but my dear friend even if i write it . It still gives Error
Dont Know Why
 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi navenn,
see u need to write some code in the try statement which can throw an IOException.If u write Exception or throwable instead of IOException in your catch block,your code will run fine.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It will not give compile error if you catch any RuntimeException or its subclasses(bcoz a RuntimeException may occur for any running program). Since Exception is super class to RuntimeException you didnt get error. Even if you catch RuntimeException it will not give any error.
If you try to catch an exception (like IOException)which might occur only for a certain type of statements, compiler objects, bcoz there is no such statement.
Try using any subclass of RuntimeException, compiler won't object.
I hope it helps.
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Naveen.
Santosh's answer is almost true. Actually I would describe it as : All checked exceptions should be actually thrown in try block before they can be used in catch block.
Since IOException is checked exception and Exception is not a checked exception it explains the scenario of code being compiled in one case and not in other. Since Java compiler gets more picky about checked exception it doesn't allow the code to be compiled.
Hope it helps.
regards
Gaurav Mantro
------------------
http://www.mantrotech.com
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unchecked exceptions ie., subclasses of Error and RuntimeException usually indicate unrecoverable error conditions. You could catch them in the program, catch them in the program but may not be able to do much in terms of graceful exit!
------------------
Ajith Kallambella M.
Sun Certified Programmer for the Java�2 Platform.
IBM Certified Developer - XML and Related Technologies, V1.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This might be useful:
Runtime Exceptions--The Controversy, part of the Essential Java Classes trail of the Java Tutorial

[This message has been edited by Dave Terrian (edited April 27, 2001).]
reply
    Bookmark Topic Watch Topic
  • New Topic