Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exceptions

 
Nisheeth Kaushal
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
In the mock question below why only answers c,d are right
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();
}
}
I mean why b,e are illegal, does a method cann't throw two exceptions at the same time and in the same statement.
And why e is wrong. It can only explain that i only need to throw runtimeexception.
Please reply urgently.
Nisheeth
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey your hierarquy is faulty.
This is the real one and you can't change it, only add more subclasses.

+ java.lang.Throwable
|
+ java.lang.Exception
|
+ java.lang.RuntimeException
I mean if the last exception is RuntimeException its supertypes must be the ones shown.
Please use http://www.javaranch.com/ubb/ubbcode.html to properly post example programs. Thanks
 
Roopa Bagur
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You only have to handle checked exceptions(all subclasses of exception except runtime exception) in your code. You don't have to handle Runtime Exceptions at all..
Anybody correct me if I am wrong

Originally posted by Nisheeth Kaushal:
Hi all,
In the mock question below why only answers c,d are right
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();
}
}
I mean why b,e are illegal, does a method cann't throw two exceptions at the same time and in the same statement.
And why e is wrong. It can only explain that i only need to throw runtimeexception.
Please reply urgently.
Nisheeth

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

yes roopa, you are correct. May be on your choice of word...I think when we say an exception is handled, doesn't it mean a problem is taken care of?... Here what you are doing is "declaring" that the method might throws an exception...you are telling the calling method "I am telling you this method you are calling may throw an exception...but it is not my problem...you do what you want with it..."
I think an exception is said to be handled when it is put in a try, catch, finally (optional) statement and is given a lasting solution.
Roopa, everyone...correct me if I am wrong.
Girmay nagesh

Originally posted by Roopa Bagur:
You only have to handle checked exceptions(all subclasses of exception except runtime exception) in your code. You don't have to handle Runtime Exceptions at all..
Anybody correct me if I am wrong


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic