• 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

Exception

 
Ranch Hand
Posts: 582
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear all,
I have a problem when i take an exam mock
What is wrong with the following code written in a single file named TestClass.java?
class SomeTrowable extends Throwable{}
class MyThrowable extends SomeThrowable{}
public class TestClass
{
public static void main( String[] args ) throws SomeTrowable
{
try{
m1() }
catch ( SomeThrowable e )
{ throw e; }
finally{ System.out.println( "Done" ); }
}
public static void m1() throws MyThrowable
{
throw new MyThrowable();
}
}
Select 2 correct answer
a. The main declare that it throws SomeThrowble but throws MyThrowable
b. you cannot have more than 2 classes in one file
c. The catch block in the main method must declare that it catches MyThrowable rather than SomeThrowable
d. There is nothing wrong with the code
e. 'Done' will be printed
the answer is d and e
Why is option a is wrong? Can you help me?
thanks
daniel
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The main declare that it throws SomeThrowble
This is true.
but throws MyThrowable
At runtime e contains a MyThrowable object. And the quesion is about what happens with this particular code. So at runtime, main() will throw a MyThrowable object.
So as far as I can see, "a" should also be a correct choice.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmmm.
lets take answer a and work through it:
a)The main declare that it throws SomeThrowble .. (so far this is correct, the main method does declare that it throws SomeThrowable..
but throws MyThrowable ..(you can see this is not correct since inside the catch statement it throws e, which is of type SomeThrowable.
hope that helps
 
Axl Rose
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ohh, and one more advice to you..
most of these mock exams on the net are written by people who don't have enough experience in the Java language, and they didn't set the exam themselves..now obviously this question asks you a question that require you to only look at the code and, see what is being written, then answer the question.. it doesn't go deep into making you think about what happen at runtime or at compile time or if the exception is a subclass of a parent exception.... etc.
thats why the answer is simple and doesn't have much thinking into it. (which is not the case in the real exam).
so next time if you see a mock exam question that doesn't make sense to you, don't think that they are correct and you are wrong, ask your self what is the background of the person who have written the question first.
 
reply
    Bookmark Topic Watch Topic
  • New Topic