Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

throws RuntimeException

 
Alfred Kemety
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given that method aMethod() throws BaseException, SubException and RuntimeException of the following exception hierarchy
java.lang.Exception | + - - BaseException | + - - SubException | + - - java.lang.RuntimeException
Which of the following are legal
A. public class MyClass {
public void myMethod(){ aMethod(); }}
B. public class MyClass{
public void myMethod() throws BaseException,RuntimeException{ aMethod();}}
C. public class MyClass{
public void myMethod() throws BaseException{
aMethod();}}
D. public class MyClass{
public void myMethod() throws Exception{ aMethod(); }}
E. public class MyClass{
public void myMethod() throws RuntimeException { aMethod();}}
Answer is C, D
Why isn't B legal???
 
Mocha D'Late
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my take : the order of the exception classes in throws matters. BaseException is 'broader' than RuntimeException, so need to be in reverse order :
public class MyClass{
public void myMethod() throws RuntimeException{ aMethod();}}
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The order doesn't matter. Furthermore, the following program compiles without error, so B is correct. Why do you say it isn't?

[ October 27, 2002: Message edited by: Ron Newman ]
 
Alfred Kemety
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not me that say it isn't.. the mock exam at this URL says it isn't..
http://valiveru.tripod.com/java/jvaltest.html
One more question, Are objects of java.lang.Math immutable?? Actually are there objects of java.lang.Math?? I know it can't be instantiated, or shall I consider that a Math object can be of actual type Integer or Long and then I can say that objects of java.lang.Math are immutable??
how about java.lang.Object? is it immutable too?
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've found an error in that mock exam. I suggest following the instructions at the bottom of that page:

Despite our sincere efforts and thorough scrutiny this mock exam may contain errors, do mail us (valiveru@mail.com) if you find one, mentioning the question and details of the error.

You can't instantiate an object of type Math, so it doesn't really matter whether it is immutable or not. Integer and Long are not subclasses of Math, they are subclasses of Number.
I don't think it's meaningful to say that Object is immutable or mutable.
 
Alfred Kemety
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ops, missed with Math and Number, but one of the questions was asking which classes are immutable, and Object and Math was in the choices, so I was wondering
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic