This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Applying a Multi-catch Block - Sybex CSG 11 p.420 Kindle Edition

 
Ranch Hand
Posts: 45
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Boyarsky, Jeanne; Selikoff, Scott. OCP Oracle Certified Professional Java SE 11 Developer Complete Study Guide (p. 420). Wiley. Kindle Edition.

In a subchapter: Applying a Multi-catch Block


Under this code:



Authors wrote this:

How can you reduce the duplicate code? One way is to have the related exception classes all inherit the same interface or extend the same class. For example, you can have a single catch block that just catches Exception. This will catch everything and anything.


I don't understand how would inheriting the same interface help here? I cannot use interface in the catch clause. If they extend the same exception, then they will be handled by the same block, I can understand that.

There is too much left untold and left to the user to ponder how one would solve it with interfaces...
 
Sheriff
Posts: 26957
84
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rade Koncar wrote:I cannot use interface in the catch clause.



Why not?
 
Rade Koncar
Ranch Hand
Posts: 45
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Why not?



Hi Paul, I try this code:



My IDE objects if I use interface in the catch clause.
 
Paul Clapham
Sheriff
Posts: 26957
84
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And if you write



?

(I too haven't ever encountered the idea of an exception implementing an interface, I'm just suggesting what to try if you wanted to do that. If it's even possible.)
 
Rade Koncar
Ranch Hand
Posts: 45
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, I get the same error.



Since exceptions are defined as classes they can implement an interface, but I wonder what the authors meant by this...

It is a very uncommon practice, this I why I got interested in the first place
 
Paul Clapham
Sheriff
Posts: 26957
84
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rade Koncar wrote:Since exceptions are defined as classes they can implement an interface, but I wonder what the authors meant by this...



Then I'd agree with you. I never thought of having a class extend Exception and also implement an interface, but there's no rule against it so why not? But apparently that isn't useful, at least not for catching those exceptions. Unless both of us are missing something, which is still possible.
 
Marshal
Posts: 74687
335
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you can make an exception implement an interface (e.g. Serializable, which most exceptions already implement). But you cannot catch it as Serializable; there is no way to know that a Serializable is an exception class.
 
Rade Koncar
Ranch Hand
Posts: 45
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Campbell, thanks for chipping in! Perhaps you can help us unveil what the authors had in mind when they suggested using interfaces?
 
Paul Clapham
Sheriff
Posts: 26957
84
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In that example you can certainly reduce the code like this:



In fact I though that's what "multi-catch" actually referred to. No idea where interfaces can be used here, though.
 
Campbell Ritchie
Marshal
Posts: 74687
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think you can use an interface; there is no such thing as an interface that is a subtype of Throwable, and whatever type you use in the parameters to the catch must be a subtype of Throwable.

Readers are reminded that I said, “subtype,” in its non‑strict sense; every type is a non‑strict supertype and a non‑strict subtype of itself.
 
author & internet detective
Posts: 40913
840
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rade,
Good point. It can't work with an interface. I've added to the errata and given you a cow.
 
Rade Koncar
Ranch Hand
Posts: 45
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, I was distracted by my job for several weeks, this is the best present/reminder to finish prepping for the exam!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic