Exceptions and Inheritance

     

Look. Let's be honest. I don't like this anymore than you. I just want to go go go. Maybe get a truck and drive. Out on the open road. I look out at the pool and it looks fantastic. I know that it's ice cold. I know it is a burning freeze of water. But it looks so serene and inviting . Maybe I could like being a giant block of ice. Drop in and freeze and then bob up and down, forever, like a cat dangling its tail on a fence, lazy, slow, back and forth. This clock wouldn't tick anymore. In a meaningful way, for me, anyway.

To:=?big5?q?JavaGarage=A4=A4=A4=E5=A5=CE=?=<java-mid.ea-@lists.javagarage.dude>

Subject: excpetion inheritance

From: "Shoeyong" <shoeyong@sonuvagun.com>

Date: Wed, 15 Dec 2003 22:47:49 +0800

Message-id: <002941c18965$4cdf7666$0100a8c0@uyong>

Old-return- path : shoeyong@sonuvagun.com

Sender: Totally Fokd <sauroman@evilempire.org>

I have come to an impasse in my evil plan to rule the world. I need to know about inheritance and exceptions.

OK. What's the name of your box?

"Boomdeay". What difference does that make?

None. I just like to know the names of guy's boxes. What is el problem?

How does it work, this thing you call method overriding and the exceptions methods throw?

Are you on something dude? An overriding method cannot be declared to throw checked exceptions EXCEPT those that are declared in the superclass method OR subclasses of those checked exceptions.

Example pls?

Say you have a super class that defines

 

 void myMethod() throws FileNotFoundException... 

Then all of these alternatives in the subclass are ok:

 

 void myMethod() throws FileNotFoundException 

the following are all NOT ok:

void myMethod() throws PsychoticBreakException //not ok: this is not in the IOException hierarchy

void myMethod() throws FileNotFoundException, EOFException //not ok: the overriding method cannot throw methods that aren't thrown by the overidden method

void myMethod() throws IOException //not ok! IOException is higher up the inheritance hierarchy than FileNotFoundException, which the superclass throws

Explain it to me. This is very good

IOException is the mama exception up the hierarchy tree of exceptions having to do with input/output ops. EOFException (End of File), and FileNotFoundException, for example, extend IOException. FileOutputStream constructors throw FileNotFoundException.

Let's say it again in a slightly different way, since it can be confusing at first.

So say you have this method "someMethod" in superclass "ClassA".

someMethod() throws IOException.

"ClassB" extends ClassA.

Now you override someMethod() in ClassB. What can it do? What can it not do? What must it do?

It can declare that it throws IOException, just like the superclass method.

It CANNOT throw *more* exceptions like this: someMethod() throws IOException, PsychoticBreakException

It CAN throw *fewer* exceptions, like this: someMethod() { } //no 'throws'.

Step right up and test your deductive reasoning skills!

True or False: If a superclass method throws no exceptions at all, an overriding method in a subclass may not throw any exceptions either.

True!

Now if you are familiar with C#, exceptions are different in Java. You are not forced in C# to handle exceptions that you aren't interested in. Java forces you to do that. So it adds a little more code, but it keeps your API exposed and documented as it should be, and makes your program more easily readable.

Cheers,

TF

This message was re-posted from java-chinese-big5@lists.javagarage.dude and converted from gb231 to big5 by an automatic gateway. Ha ha ha ha.



Java Garage
Java Garage
ISBN: 0321246233
EAN: 2147483647
Year: 2006
Pages: 228
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net