• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Exceptions and overridden methods

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code id long, but look only at the bold part.Is it true that when you upcast you must catch the exceptions from the base-class version of the method??
I changed the codeto
and it works fine.

[ August 18, 2007: Message edited by: Piotr Milewski ]
[ August 18, 2007: Message edited by: Piotr Milewski ]
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy!



Exceptions:
BaseballException <- Foul <- PopFoul
BaseballException <- Strike
StormException <- RainedOut

other classes:
Inning:
Inning <- StormyInning
constructor Inning() throws BaseballException
method atBat() throws PopFoul
StormyInning
constructor StormyInning() throws RainedOut, BaseballException

Inning i = new StormyInning();
i.atBat();
version one caught: Strike, Foul, RainedOut, BaseballException.
version two caught: Strike, PopFoul, RainedOut, BaseballException.

It will work in any case.
Because in the last catch, you catch the BaseballException that covers them all, so it compiles.
You may also simply catch only the BaseBallException to compile.



Yours,
Bu.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Piotr Milewski:
...I changed the code

to

and it works fine.


This works because Foul extends BaseballException, so if a Foul is thrown, it will be caught by the catch block for BaseballException.
 
I guess I've been abducted by space aliens. So unprofessional. They tried to probe me 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