wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exceptions" Watch "Exceptions" New topic
Author

Exceptions

Sudipta Das
Greenhorn

Joined: Apr 05, 2001
Posts: 18
Which is the correct answer for this quiz ?

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");
}
}
1.Compiler error complaining about the catch block where no IOException object can ever be thrown.
2.Compiler error - IOException not found. It must be imported in the first line of the code.
3.No compiler error. The lines "Before Try" and "At the end" are printed on the screen.
Adrian Yan
Ranch Hand

Joined: Oct 02, 2000
Posts: 688
2 maybe?
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
The answer is 1.
Again, why didn't you just compile and run it? Wouldn't that have been faster than asking here?
------------------
Moderator of the Programmer Certification Forums


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Art Metzer
Ranch Hand

Joined: Oct 31, 2000
Posts: 241
I notice this series of questions comes from this mock exam, out of Maha Anna's list. As it says on Abhilash's Answers page, Sudipta, if you E-mail Abilash at java.quiz@mymailbag.com, he will be more than happy to E-mail you explanations of all the answers. Trust me, I've done it. I highly recommend that you do so, too, Sudipta. Then, after reviewing those explanations, if you still have questions, feel free to consult your fellow ranchers.
It has been my experience on JavaRanch that the depth and amount of help you receive is directly proportional to the amount of effort you have demonstrated putting forth to solve your particular problem.
Art
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
By the way, 2 is wrong because the package was specifically listed with the IOException.
Zkr Ryz
Ranch Hand

Joined: Jan 04, 2001
Posts: 187
Originally posted by Thomas Paul:
The answer is 1.
Again, why didn't you just compile and run it? Wouldn't that have been faster than asking here?

I have found useful that other people post this and all kind of questions, because it helps on my preparation.
Compiling and runnig, would have gave him an answer, but I would not ever notice about such question.

James Du
Ranch Hand

Joined: Mar 23, 2001
Posts: 186
Hi all, I quoted the code here for convenience

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");
}
}

I've tyied the code and finded that the answer is 1 and that the reason is not so simple as i assumed.
I can't find a rule in the specification says that the exception in the catch clause necessarily can ever be thrown in the try clause, is there any?
I looked up the specification and find there's a seemingly-related reason, pls. look at the following quote from the spec.

A catch block C is reachable iff(if and only if) both of the following are true:
Some expression or throw statement in the try block is reachable and can throw an exception whose type is assignable to the parameter of the catch clause C. (An expression is considered reachable iff the innermost statement containing it is reachable.)
There is no earlier catch block A in the try statement such that the type of C's parameter is the same as or a subclass of the type of A's parameter.

It seems that the statement in the catch clause is considered unreachable by the compiler so it complained, but I find if I replace the "java.lang.IOException" with "ArithmeticException", the compiler doesn't object to the code any more, why? just because "ArithmeticException" is the subclass of "RuntimeException"? Does it mean that unreachable statement could be acceptable in some special case or that an empty block(i.e {} ) is considered a potential RuntimeException thrower?
I really need a clarification, thanks in advance.
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Yes, you're right. RuntimeException and its subclasses are not checked exceptions ie., the code is not expected either to declare or to catch the exception.
HTH


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
James Du
Ranch Hand

Joined: Mar 23, 2001
Posts: 186
Hi Ajith,
I wonder at what point i'm right?
Does it mean that unreachable statement could be acceptable in some special cases(e.g. the unreachable statement in the catch clause of which the exception parameter is of subclass of RuntimeException)?
Or that an empty block(i.e {} ) is considered a potential RuntimeException thrower? in this case, the statement enclosed in the catch clause is not considered unreachable.
Regard.
James
Naveen Sharma
Ranch Hand

Joined: Mar 23, 2001
Posts: 65
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
try{}
catch(Exception e) //Compiles and Run
Please Explain Why So..
Thanks
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Exceptions
 
Similar Threads
Try catch block.....
pls have a look at following two examples of the same kind of question
q from abhilash's mock exam on try catch
can anyone explain this try catch
Why so much difference between two similar class?