Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

checked exceptions  RSS feed

 
pras
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Writer1 { //static String s;
public static void main(String args[]) throws IOException {

boolean newFile=false;
File file=new File("abcd");

/* int a=23/0;
if(s==null)
System.out.println(s.length()); */


newFile=file.createNewFile();
System.out.println(newFile);
}
catch(Exception e) { e.printStackTrace();}

System.out.println("hi thata");
}

hi can anyone tell me one example where i can catch IOException suppose i had written a catch clause for IOException. real example. not theory. please.
 
Leandro Melo
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.

I'm not sure if this is what you're looking for...

 
pras
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi. i want a scenario where IOException is caught.
 
Bill Cruise
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An IOException will be thrown if you try to read from an InputStream that has been closed.

For more examples, google for "throws IOException" to find classes that actually do this.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by prasanna sheregar:
hi. i want a scenario where IOException is caught.


You are studying for SCJP and do not know how to catch an exception? In this case I will move this question to our beginner's forum...
 
Gregory Joseph
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think what Prasanna is trying to ask is - in what scenario an IOException is actually thrown by the file.createNewFile() function. This question might have come to his mind because even if a file with the same name already exists in the directory in which we are attempting to create the new file, an exception is not raised, instead without creating the new file the function returns the boolean value false.

Well if I have interpreted your question correctly then I think the answer is that there are many exceptional situations in which an IOException could be raised while attempting to perform disk I/O. If you look at the direct subclasses of IOException in the Java API specification you might be able to understand specific scenarios in which an IOException can be thrown from the file.createNewFile() function. For eg an InterruptedIOException is raised when an I/O operation is interrupted.

Believe me the API is like the Bible. Its got all the answers. Hope this helps you .

Regards,
Gregory Joseph - On the verge of giving SCJP 5.0 .
 
pras
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Gregory Joseph for your help.

so if the file already exists and we try to create new file it returns false and not an IOException?

And give me one scenario in which IOexception is caught?

give me the names of some more checked exceptions?
whatis the parent class of checked exceptions?
 
Gregory Joseph
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you see the signature of createNewFile(), its return type is boolean so that if the file is ceated successfully then the funcion returns true and if a file with the same name exists due to which the file could not be created then it returns false. You can check this out by:
System.out.println("File created = "+file.createNewFile());

I have never faced an IOException while creating a new file so i dont know when the exception is actually thrown... but i presume that it can be raised if the OS interrupts during file creation or if we are attempting to create the file on a remote machine then there can be some failure in the communication channel, socket or access violations.

All the checked exceptions extend from the java.lang.Exception class(there is no class called CheckedException)
For eg:

java.lang.Object
java.lang.Throwable
java.lang.Exception
java.io.IOException

Additionally RuntimeException which is a class extends from java.lang.Exception and all the runtime exceptions like NullPointerException extend from RuntimeException.

java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
java.lang.NullPointerException

If you dont have the java api then you can download the same in html help format(easy to use and search) from the following link:
Javadocs download

Regards,
Greg.
 
pras
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you so much. your answer was bang on target.

it cleared all my doubts.


thank you
 
pras
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.io.*;

class Directory2 {
public static void main(String args[]) {

try {
File dir=new File("prasdir");
// dir.mkdir(); /* code here is the culprit */
File myfile=new File(dir,"rocket.txt");

myfile.createNewFile();

} catch(IOException e) { e.printStackTrace();}
System.out.println("hi");
}
}

hi guys and girls .

i have one more doubt.

in the above code i hava commented the code where actual directory is created. but when i run the program it compiles fine and gives a IOException at runtime. but according to checked excptions concept it should report an error at compile time? dont you people think so?

regards,
prasanna.s.k
 
Anupam Bhatt
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasanna,

those exceptions which may be thrown from a method, and should be declared as a part of the method signature or should be caught, are checked exceptions.

So if you do not catch the IOException in your code, compilation will fail saying that you need to specify the "throws" exception in the method signature OR should be caught in a catch block.

This allows the callers of the method to know that the method being called could throw and exception and they chould prepare to catch it, or rethrow.

In your case, the exception is thrown at runtime [since at compilation no check is done to see whether that directory exists or not.], which you catch it and print the stack trace on the console.

To put in short, checked exceptions are those which should be specified to be thrown in the method signature or should be caught in the code, signalling that such kind of exception could occur at runtime.

I hope i haven't confused you more.
 
Gregory Joseph
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Prasanna,
I am afraid that your understanding of checked exceptions is a bit off the mark. Checked Exceptions are those for which the compiler checks at compile time to see if we have handled( using try - catch) or declared( using throws clause) the exception. If some part of our code can raise a checked exception and we have neither handled it nor declared it then there is a compiler error. As long as we have done either one(handle or declare) the compiler is happy. But the keyword is can. Its not necessary that the exception has to be raised. Incase the exception is raised which happens at runtime then the VM handles it in the way we have specified.
So checked exceptions are not raised at compile time. The compiler just checks to ensure that checked exceptions are declared or handled at compile time. If an exception like IOException is actually raised then this will happen only at runtime and the VM will proceed to execute the catch block that we have specified for the particular exception.

Regards,
Gregory
 
Bharat Makwana
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Run this program, you will get IOException

class Test {

public static void main(String[] args) {
B b = new B();

try {
ObjectInputStream oos = new ObjectInputStream(new FileInputStream(new File("hi.ser")));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
System.out.println("IOException...");
}


}
}
 
Gregory Joseph
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bharat,
Could you please explain why you feel that an IOException will be thrown at runtime.

In the previous code you will get errors at compile time because java.io has not been imported and I dont know from where B b=new B() came from.

Once you have resolved that you will get a FileNotFoundException at runtime because there will be no file called hi.ser in your folder.

After creating a file hi.ser manually you will get an IOException because an EOFException is raised as the file is empty.

If you enter some junk data into your file hi.ser and run the code then you will get an IOException because a StreamCorruptedException is raised as the file has been opened by an ObjectInputStream and the data in the file is junk and does not have proper headers.

That was a difficult example of IOException being raised!!!

Regards,
Greg
[ June 25, 2007: Message edited by: Gregory Joseph ]
 
Bharat Makwana
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello Gregory
 
pras
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Gregory Joseph .

your way of giving expalnation is great.

i understand every word you say.

you have a PERFECT way of answering queries.


regards,
PRASANNA.S.K
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!