• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

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

 
Ranch Hand
Posts: 46
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: 28383
99
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: 46
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: 28383
99
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: 46
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: 28383
99
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: 80485
455
  • 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: 46
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: 28383
99
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: 80485
455
  • 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: 42109
934
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: 46
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!
reply
    Bookmark Topic Watch Topic
  • New Topic