• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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.
 
Quick! Before anybody notices! Cover it up with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic