DIP33: A standard exception hierarchy

H. S. Teoh hsteoh at quickfur.ath.cx
Tue Apr 2 09:54:36 PDT 2013


On Tue, Apr 02, 2013 at 09:43:20AM -0400, Andrei Alexandrescu wrote:
> On 4/2/13 7:59 AM, Dmitry Olshansky wrote:
> >02-Apr-2013 15:35, Andrei Alexandrescu пишет:
> >>On 4/2/13 7:24 AM, Dmitry Olshansky wrote:
> >>>You might want to add Visitor pattern to Exceptions but it's darn
> >>>messy to deal with and is an overkill most of the time.
> >>
> >>Actually I think that's a good thing to do.
> >>
> >
> >Why would be that? It doesn't solve the key problem of "try clause
> >plus a ton of semi-identical catches" used just to perform a mapping
> >of X handlers to Y subsets of errors. Plus visitor does the same
> >dispatch that is already addressed by exception handlers (or partly
> >so).
> 
> Visitor allows centralized and flexible handling of exceptions.
> 
> >If somebody comes up with a reasonable Visitor pattern for Exceptions
> >that is flexible and fast then sure let's see it. I just doubt it'll
> >help anything on its own in any case.
> 
> Well I think exceptions + factory + visitor is quite the connection.
[...]

Hmm. So one could have something like this, perhaps?

	void main() {
		try {
			dotDotDotMagic();
		} catch(Exception e) {
			e.handle(ExceptionHandler());
		}
	}
	class ExceptionHandler {
		void handle(IOException e) { ... }
		void handle(ParseException e) { ... }
		...
	}

Of course, this would be the lowered version of some nice syntactic
sugar that the compiler would translate into.

But I'm not sure how this offers an advantage over the current state of
things, though. How would you handle the case where some exception types
aren't handled by ExceptionHandler? How would you handle accepting
multiple exception types by the same handler code? How would you map
something like this to nicer syntax that isn't worse than the current
way of just manually defining a common function (or overloaded
functions) that handles the exception?

Or perhaps I didn't quite understand how you intend to implement the
visitor pattern in exceptions.


T

-- 
Knowledge is that area of ignorance that we arrange and classify. -- Ambrose Bierce


More information about the Digitalmars-d mailing list