• 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

Exceptions

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
2 maybe?
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, 2 is wrong because the package was specifically listed with the IOException.
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
James Du
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
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
try{}
catch(Exception e) //Compiles and Run
Please Explain Why So..
Thanks
 
Could you hold this kitten for a sec? I need to adjust this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic