• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Internal type of variable in multi-catch

 
Ranch Hand
Posts: 241
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the internal type of the variable in a multi-catch really java.lang.Exception? Boyarsky & Selikoff's OCP Study Guide p. 295 seems to suggest so,

Java uses the common Exception superclass for the variable internally.



but how come the code below compiles and runs:
(awful catch of an Error there, I know, but this is just an experiment to learn how the JVM behaves)


Doesn't this suggest the internal type is either Throwable or Object? Anyway, the point here was to show that the variable e is implicitly final, which it is, regardless of what type Java uses for the variable internally.
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
getClass() returns the runtime type of the object. The common superclass is used as the formal type (the compile-time type). It would be as if you had written catch (Throwable e), except it doesn't catch Throwables that aren't IOException, DateTimeParseException or Error.
 
T Vergilio
Ranch Hand
Posts: 241
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Stephan,

I understand. But Error is not an Exception. This is why I printed the result of getClass(), i.e. to show that the type of the variable can't be Exception.

Don't you agree?

 
T Vergilio
Ranch Hand
Posts: 241
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:It would be as if you had written catch (Throwable e), except it doesn't catch Throwables that aren't IOException, DateTimeParseException or Error.



Exactly! I'd assume the internal type is a Throwable, but I don't know for sure.
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think in most articles Exception and Throwable are used interchangeably.
 
T Vergilio
Ranch Hand
Posts: 241
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fair enough, I don't think it's important to know what it is anyway
 
T Vergilio
Ranch Hand
Posts: 241
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I can answer my own question here. The book is correct, since the example it is referring to is:

I found this digging through the Java Specification:

The declared type of an exception parameter that denotes its type as a union with alternatives D1 | D2 | ... | Dn is lub(D1, D2, ..., Dn).


For the example in the book, the least upper bound superclass is Exception.

For my example, it was Throwable.

All is good
 
You don't like waffles? Well, do you like this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic