• Post Reply Bookmark Topic Watch Topic
  • New Topic

Catch blocks can be polymorphic.  RSS feed

 
Shubham Semwal
Ranch Hand
Posts: 176
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are catch blocks polymorphic.
Please help with an easy example.
Thanks
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't know they could be. Why don't you post an example of what you think might be a polymorphic catch block and let's see what people here think about it?
 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Head First Java, 2nd Edition, page 348 says
catch blocks can be polymorphic

On page 330, it explains this statement by saying that Exceptions can be referred to polymorphically.

Similarly, you can say that a reference is polymorphic if it can refer to subtypes.

page 330 says
The benefit for exceptions is that a method doesn’t have to explicitly declare every possible exception it might throw; it can declare a superclass of the exceptions. Same thing with catch blocks-you don’t have to write a catch for each possible exception as long as the catch (or catches) you have can handle any exception thrown.

page 331 says
Just because you CAN catch everything with one big super polymorphic catch, doesn’t always mean you SHOULD.

In other words, even though catch(Exception e){} can catch all Exception subtypes, it is advisable to avoid this code
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Bishara wrote:On page 330, it explains this statement by saying that Exceptions can be referred to polymorphically...

Hmmm. I dispute that. Because IMO it's a corruption of the term "polymorphic" - which has a very specific meaning (at least to me).

Polymorphism - from the Greek for "many" (poly) and "change" (morph) - is the ability of a supertype to change behaviour depending on the runtime type of an instance, without the user having to write any dispatch code. At least that's the way I've always understood it.

All catch does is allow you to specify which subclass of Throwable a particular block is interested in - which might make it "polytypic", but certainly not "polymorphic".

In essence, it's equivalent to a bunch of
  if (e instanceof [whatever]) { ...
statements, which IS dispatch code.

So I'd actually say that catch is the antithesis of polymorphism.

Any takers?

Winston
 
salvin francis
Bartender
Posts: 1663
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's my take on this. ..

Form 1: Catching multiple exceptions with multiple catch blocks


Form 2: Catching More Than One Type of Exception with One Exception Handler


code pasted from : https://docs.oracle.com/javase/tutorial/essential/exceptions/catch.html

Though even I personally think the term is a bit misleading... I tend to think of the two forms as above
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:from the Greek for "many" (poly) and "change" (morph)

Oops, my mistake. Morph means "form", not "change" - although in modern idiom it's often used in the latter context (eg, "morphing").

And IMO the power of Object-Orientation comes from the latter meaning, since it would be kind of self-defeating for an OO language to allow subtyping, but make all methods final.

Winston
 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:In essence, it's equivalent to a bunch of
  if (e instanceof [whatever]) { ...
statements, which IS dispatch code.

Yes, polymorphism helps us avoid using a bunch of instanceof operators in our code. Polymorphism helps us write code that doesn’t have to change when we introduce new subtypes into our program. According to the book, with catch blocks, you don’t have to write a catch for every possible Exception subtype, which as you said is equivalent to using a bunch of instanceof operators. In other words, even though the book discourages it, it is possible to write code like this

The specific Exception subtype thrown at runtime will be handled by this catch block at runtime. This is what I think the book means when it says that catch blocks can be polymorphic.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Bishara wrote:According to the book, with catch blocks, you don’t have to write a catch for every possible Exception subtype...

Yes, I can see it from that point of view; it's just never the way I've never understood the term.

But it could be the way I learnt it. Wikipedia seems to equate the terms "subtyping" and "polymorphism", which to me are two completely distinct things. It also includes things like overloading - which I've never thought of as polymorphism - and generics - which actually does make sense to me.

So maybe it's just my screwed-up interpretation of the word...but I don't think I'm alone.

Winston
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:. . . Wikipedia seems to equate the terms "subtyping" and "polymorphism" . . .
But everybody knows Wikipedia is full of errors. It is usually better than that for computer science, however.
 
Shubham Semwal
Ranch Hand
Posts: 176
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I always thought that polymorphism is "overriding" in java.
As per the discussion examples involving subclass are polymorphic right ?

 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See, your question is really when you get right down to it "What is polymorphism?"

And as you can tell from the discussion here, there isn't one definition which says exactly what polymorphism is in computer programming. So sit back and follow the discussion but you aren't going to end up with a precise definition of polymorphism.

Me, I know that catch(Exception e) will catch all subclasses of Exception. Is that polymorphism? I don't care. I know how the construct works and if people want to apply names to it, then that's fine as long as they don't try to force me to validate their choice of names.
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But anyway I've given you a cow for kicking off an interesting discussion.
 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This thread is really about polymorphic arguments and whether a method whose arguments are dynamically resolved at runtime can be said to be polymorphic. According to the book, you can write a polymorphic method that uses polymorphic arguments (where you declare a method parameter as a supertype). What makes the method polymorphic is that the JVM can pass in any subtype object at runtime. Thanks to polymorphism, you can write a polymorphic method, go on vacation, and someone else can add new subtypes to the program and your polymorphic method will still work with the new subtypes.

This code prints

Since the final giveShot(Animal a) method uses a polymorphic argument and calls an overridable method, the JVM can pass a Dog or a Hippo object at runtime. We can say that the final giveShot(Animal a) method is polymorphic because it can be passed a Dog or a Hippo object at runtime i.e. its argument can be dynamically resolved at runtime.

Similarly, since a catch block's argument can be dynamically resolved at runtime, it can be said to be polymorphic.
 
Shubham Semwal
Ranch Hand
Posts: 176
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for an interesting discussion and thanks @Paul for the Cow
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:Oops, my mistake. Morph means "form", not "change" - although in modern idiom it's often used in the latter context (eg, "morphing").


Actually "morphing" comes from "metamorphosis", or "change of form". "Meta" means change.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!