• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

throws RuntimeException

 
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();}}
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
It's never done THAT before. Explain it to me tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic