• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Exam Cram Question

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi & Good Morning,

Exam Cram has a question :
Exception
+-----BadTastException
+--------BitterException
+--------SourException
BadTaste Exception is defined as an abstract class.
You have a method eatMe that may throw a BitterException or a SourException.Which of the following declaration will be acceptable to the compiler?
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
BadTesteException
d) public void eatMe(Ingredient[] list)

the answer is given a) & b)
I thought it should be only b).
can an abstract exception be thrown?
can any of u pls help me in getting out of this doubt.
thanks & regards
Sathi
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes the answer a is legal.
You're not throwing an abstract exception. You are throwing an exception of type BadTasteException, which you can not instantiate an object of, but you can instantiated objects of types BitterException and SourException which are subclasses of BadTasteException. It is perfectly legal to declare a method throws a parent class (abstract or not), but the only practical types thrown from it are actually subclasses of the parent class.
Hope this helps.
 
Sathi Chowdhury
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Brett.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I might add that understanding how you might treat the same object with a reference to an interface, an abstract class or a parent object is ABSOLUTELY ESSENTIAL to programming in Java.
As I recall, checking for this understanding is one reason I wrote that question in the first place.
Bill

------------------
author of:
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic