Jump to content
Wagner Landgraf

Exception classes implementing interfaces

Recommended Posts

Just out of curiosity, I wonder if anyone implements interfaces from Exception classes? Like:

EMyException = class(Exception, IMyInterface)

Of course this would require interfaces would be non-referenced counted, but it would be a way to add arbitrary information to exceptions without having to inherit them from a specific class.

Share this post


Link to post
12 minutes ago, Wagner Landgraf said:

Just out of curiosity, I wonder if anyone implements interfaces from Exception classes?

I've never seen anyone do it, or even suggest it.  But, I don't see any reason why it couldn't be done.

  • Thanks 1

Share this post


Link to post
4 hours ago, Remy Lebeau said:

I don't see any reason why it couldn't be done.

Yes, it probably could be done, but should it?

It's not a bad idea, but without low level compiler support (on E: IMyError do ... ) I wouldn't do it. Personally I want my error handling to be as simple and robust as possible so it doesn't potentially make a bad situation worse.

  • Like 3

Share this post


Link to post
30 minutes ago, Anders Melander said:

Yes, it probably could be done, but should it?

It's not a bad idea, but without low level compiler support (on E: IMyError do ... ) I wouldn't do it. Personally I want my error handling to be as simple and robust as possible so it doesn't potentially make a bad situation worse.

It's an interesting approach when you have a centralize exception handler/processor mechanism - like logging systems, serializers, servers, etc.. It's a way to add metadata to the exception without having to be strict to a specific class hierarchy.

Edited by Wagner Landgraf

Share this post


Link to post

I don't see how it could help... if you want to extend any exception, incl 3rd party, they won't implement the interface. If you want to extend your own classes, why not inherit them from extended base class. Btw, you can wrap all exceptions thus extending ALL of them:

(pseudocode)
 

except on E:
  raise EExtended.Create(InnerException := E, AddInfo := ...)
end

Exception class even has the pointer to Inner exc already but I've never used it

  • Like 2

Share this post


Link to post

With EurekaLog, it is possible to capture and log EVERY exception before it is handled, so why bother with interfaces?

We use inheritance, mostly to classify exceptions for a sub-domain.   
Exception
  - ParsingException

    - InvalidCSV

    - InvalidEmptyField

try
  Parse;
except
  on E:Excepetion do 
    if E is ParsingException
     then LogSomethingInformative
      else raise.
end;

 

  • Like 3

Share this post


Link to post
17 hours ago, Wagner Landgraf said:

Just out of curiosity, I wonder if anyone implements interfaces from Exception classes? Like:


EMyException = class(Exception, IMyInterface)

Of course this would require interfaces would be non-referenced counted, but it would be a way to add arbitrary information to exceptions without having to inherit them from a specific class.

This sounds like a solution in search of a problem. As previously mentioned there are better options.

  • Like 4

Share this post


Link to post
On 10/29/2021 at 12:34 AM, Fr0sT.Brutal said:

Btw, you can wrap all exceptions thus extending ALL of them:

(pseudocode)


except on E:
  raise EExtended.Create(InnerException := E, AddInfo := ...)
end

 

You have to use Exception.RaiseOuterException() to capture an InnerException, so it would look more like this:

except
  on E: Exception do begin
    Exception.RaiseOuterException(EExtended.Create(AddInfo...));
  end;
end

Share this post


Link to post
On 10/29/2021 at 10:52 AM, Lars Fosdal said:

try
  Parse;
except
  on E:Excepetion do 
    if E is ParsingException
     then LogSomethingInformative
      else raise.
end;

While assuming that this code snippet is just to demonstrate the point, it always raises a question in my head: why trap every possible exceptions and then re-raise if it's not the one we are expecting, if we could just trap the one we are intrested in.

try
  Parse;
except
  on E: ParsingException do
    LogSomethingInformative;
end;

 

  • Like 1

Share this post


Link to post
On 10/30/2021 at 8:39 PM, Remy Lebeau said:

You have to use Exception.RaiseOuterException() to capture an InnerException, so it would look more like this:

You're right, though your solution utilizes RTL facilities while mine is fully abstract

Edited by Fr0sT.Brutal

Share this post


Link to post

Valid point. Some of my exception handlers deal with specific exception(s), as well as shared handling code. 

Hence I write it the way that I do for clarity.  An exception is already costly AF - so code efficiency is not a big deal.

 

Share this post


Link to post
1 hour ago, Lars Fosdal said:

Hence I write it the way that I do for clarity

How is

On 10/29/2021 at 9:52 AM, Lars Fosdal said:

on E:Excepetion do
  if E is ParsingException

 

more clear than

On 10/30/2021 at 8:15 PM, Dmitriy M said:

on E: ParsingException do

 

?

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×