Gregory Joseph

Greenhorn
+ Follow
since May 25, 2007
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Gregory Joseph

Hi Ranchers,
Howdy!! Completed SCJP 5.0 couple of weeks ago.. Thinking of SCWCD. What good reference material is availabe???

Cheers,
Greg
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 ]
17 years ago
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
17 years ago
Hi Jason,
Well buddy the answer to your question is the run() method is not inherited by PE14 class so there is no question of overriding happening !!! The PE14 class doesnt even know that Vehicle class has a run() method because it is marked private in the Vehicle class and private members cannot be seen even by subclasses. So the "new PE14().run();" statement just executes the run() method in the PE14 class.. Simple as that. Hope this helps answering your question. Work harder on access modifiers...

Regards,
Greg
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.
17 years ago
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 .
17 years ago