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

Exception

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exception
+-- BadTasteException
+-- BitterException
+-- SourException
BadTasteException is defined as an abstract class.
You have a method eatMe that may throw a BitterException or a SourException.
Which of the following method declarations will be acceptable to the
compiler? [Check all correct answers]
A) public void eatMe( Ingredient[] list ) throws BadTasteException
B) public void eatMe( Ingredient[] list ) throws BitterException,
SourException
C) public void eatMe( Ingredient[] list ) may throw BadTasteException
D) public void eatMe( Ingredient[] list )
what will be the output of above programme .
According to me output should be A, B , D .
but the answer is A ,B
can anybody explain me ?
thanks in advance .
rahul
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can we throw an abstract subclass of exception?
tell me why A is correct?I thought B & D should be correct
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rahul Pawar:
Exception
+-- BadTasteException
+-- BitterException
+-- SourException
BadTasteException is defined as an abstract class.
You have a method eatMe that may throw a BitterException or a SourException.
Which of the following method declarations will be acceptable to the
compiler? [Check all correct answers]
A) public void eatMe( Ingredient[] list ) throws BadTasteException
B) public void eatMe( Ingredient[] list ) throws BitterException,
SourException
C) public void eatMe( Ingredient[] list ) may throw BadTasteException
D) public void eatMe( Ingredient[] list )
what will be the output of above programme .
According to me output should be A, B , D .
but the answer is A ,B
can anybody explain me ?
thanks in advance .
rahul


Why d???
When you are defining a new subclass of Exception you are naturally creating a checked exception that means exceptions which must be caught by the Exception Handler in your class.
So the method should be conveying the message that i may be throwing the exception so that where u call the method u put it under a try catch block or else if your try catch is in a method throw it again using 'throws'.
Yah one more thing.Instead of that if you throw Superclass i;e Exception that will do since now you are saying to exception handler handle all the exceoptions of type Exception.That is the case in option 1 where u are saying deal with all the types of BadTasteException.That includes SourTaste.Even u can directly write throws Exception which though is easy to write but a very bad programmming technique unless u dont know which exceptions are going to be thrown
hope that helps
sandeep

[This message has been edited by sandeep bagati (edited March 19, 2001).]
 
crispy bacon. crispy tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic