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

Throwing exceptions... mock question doubt

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends,

please check out this question...

Given that method aMethod() throws BaseException, SubException and RuntimeException of the following exception hierarchy







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

Answers: C and D

what i dont understand is why option B is incorrect?
the quesion states "method aMethod() throws BaseException, SubException and RuntimeException ", then why cant we throw both BaseException and RunTimeException together from myMethod... the way we handle these exceptions is beyond the scope of this question right?can't myMethod choose to throw these exceptions? i know i'm missing something here.. can anyone explain???

thanks in advance
[ September 04, 2007: Message edited by: Radha Kamesh ]
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
RuntimeException are unchecked kind of Exceptions.This kind of exception is managed by JVM itself,Compiler doesnot put any obligation to throw or handle these kind of exception,However All exceptions which are derived from non - RuntimeException class are Checked Exceptions,For this we have to handle and throw these exceptions.
To get more on this refer to K&B Chap-5.

Regards
mittu
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess the option B may not be correct as the Exceptions need to be thrown in the order of hierarchy, with the base exception coming last....
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is because inheritance hierarchy. BaseException is the super class of RuntimeException and as per the inheritance rule super class can refer to any of the subclass without casting.

So here "BaseException" or "Exception" can handle any other exceptions and there is no need to define "RuntimeException" to create duplicacy. Here "RuntimeException" is nothing but the "BaseException" for compiler.
 
Radha Kamesh
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry friends, the diagram for the exception hierarchy i had posted came out wrongly.. i have now out it in a code block for the structure to be retained......

according to the diagram, now both BaseException and RuntimeException derive directly from Exception...

i know that Runtime exception need not be declared... but is it wrong to do
so in this case?
 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wrote code like



It gives me no error.

Try it by your self, You will clear.
 
I'm thinking about a new battle cry. Maybe "Not in the face! Not in the face!" Any thoughts tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic