Can we make Throwable an interface?

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Tue Dec 9 10:05:08 PST 2014


On Tue, Dec 09, 2014 at 08:06:32PM +0300, Dmitry Olshansky via Digitalmars-d wrote:
> Then we could use interfaces as "tags" for exceptions and catch using one of
> many interfaces an exception has. Consider DIP33:
> http://wiki.dlang.org/DIP33
> 
> The 2 problems it had were:
> 
> 1. enums are hard to extend for std lib, and absolutely impossible by
> 3rd party libraries.
> 2. Single hierarchy has some appeal but it doesn't allow to catch on
> similar classes that do not inherit from the same base class.
> Basically there are many ways to view similarities of excpetions and
> single hierarchy fails to address that.
> 
> If we were to replace each class with a base interface and every Kind
> enum with an interface (inhereting from one or more base interfaces)
> then we can actually address both of these points.
[...]

I like this idea!

It would also address the current messy situation with OS-specific
exceptions (e.g., ErrnoException or WindowsException, or whatever it's
called) vs. semantically-oriented logical exceptions
(FileNotFoundException, NoAccessException, etc.). OS-specific exceptions
are necessary to capture OS-specific information, but user code
generally wants to catch according to more semantic / logical criteria.
For example, dirEntries may fail due to permission failure, but the user
is generally not interested in OS-specific error codes like errno or
Windows error numbers; what is more interesting is "was this failure
caused by permission error?".

This problem cannot be satisfactorily resolved with a single Exception
hierarchy, but it *can* be resolved by using interfaces instead of base
classes. We could then have a WindowsException and a PosixErrnoException
(for example), and have subclasses also implement a NoAccessException
interface. Thus you have a class hierarchy based on implementation
details (e.g., PosixErrorException stores errno, and WindowsException
stores Windows error codes), but also an interface hierarchy based on
logical categorizations of exceptions.


T

-- 
If I were two-faced, would I be wearing this one? -- Abraham Lincoln


More information about the Digitalmars-d mailing list