This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Exceptions

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Nothing up my sleeve ... and ... presto! A tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic