• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception Handling

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have been studying from a study guide and I was faced with the following question:-

public class ExceptionTest {
class TestException extends Exception {}
public void runTest () throws TestException {}
public void test ()/* Point X*/{
runTest();

}
}

At point X on line 4, which code can be added to make the code compile?
A.throws Exception,
B.catch (Exception e)
C.throws RunTimeException.
D.catch (TestException e)
E.No code is necessary

Answer: B
I tested the code and I think the correct answer is A.


Am I right. Thanks for your help.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer B is certainly wrong. Out of the choices, A is correct.
 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Raef Kandeel:
I have been studying from a study guide and I was faced with the following question:-

public class ExceptionTest {
class TestException extends Exception {}
public void runTest () throws TestException {}
public void test ()/* Point X*/{
runTest();

}
}

At point X on line 4, which code can be added to make the code compile?
A.throws Exception,
B.catch (Exception e)
C.throws RunTimeException.
D.catch (TestException e)
E.No code is necessary

Answer: B
I tested the code and I think the correct answer is A.


Am I right. Thanks for your help.



Yes that study guide contains many answers wrong. Very obvious reason, a catch cannot exist on its own.
 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer A is correct, because method runTest throws a TestException which extends exception which is a checked exception and the rule is "handle or declare"

For to handle you need to have a catch in a try..catch block in which you need more than one line in the example
So the next option is to declare which is option A.

By throwing exception you can also throw a subclass of exception which is TestException.
 
It's never done THAT before. Explain it to me tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic