This week's book giveaway is in the Python forum.
We're giving away four copies of High Performance Python for Data Analytics and have Tiago Rodrigues Antao on-line!
See this thread for details.
Win a copy of High Performance Python for Data Analytics this week in the Python 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

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

 
Ranch Hand
Posts: 41
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...
 
Marshal
Posts: 26308
80
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: 41
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
Marshal
Posts: 26308
80
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: 41
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
Marshal
Posts: 26308
80
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: 71752
312
  • 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: 41
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
Marshal
Posts: 26308
80
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: 71752
312
  • 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: 40393
821
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: 41
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!
 
Here. Have a potato. I grew it in my armpit. And from my other armpit, this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic