Re: Try/Catch
- Posted by petelomax Jan 01, 2015
- 6488 views
I'm wondering how try/catch/finally might be implemented in Euphoria.
Raymond Chen is always worth a read:
http://blogs.msdn.com/b/oldnewthing/archive/2005/01/14/352949.aspx
http://blogs.msdn.com/b/oldnewthing/archive/2004/04/22/118161.aspx
(whatever gets implemented should address those concerns, and when that bloke says that something is hard, you really should pay attention)
First, instead exiting on an error, RTFatal() would have to have set some sort of flag.
I would think that you need some kind of stack unwind.
At the finally portion of the try/catch/finally block, things get a bit more dicey.
All excellent arguments for not adding finally (an awful, stupid, confusing concept) to Euphoria.
If the main point of exception handling is to separate error handling code from the main logic, what is wrong with putting such things in a separate routine?
procedure cleanup() <such code> end procedure try ... cleanup() catch ... cleanup() throw(...) end try
or am I missing something?
It makes me madder than hell to think of such a horrible backward construct being added to Eu, if two simple calls would do.
Of course if you are not re-throwing an exception, which I would have thought is more likely anyway, or are prepared to use a simple flag/return value, <such code> can and should just go after the end try.
In standard C setjmp and longjmp() look like they'll do the trick.
I randomly stumbled over a possibly interesting snippet from Michael Anderson:
http://stackoverflow.com/questions/10586003/try-catch-statements-in-c
(last post but one) though (as I said) a stack unwind based approach might be better/easier.
Of course the other burning question is why someone might want exception handling:
- to write good quality robust code, but without the messiness of error code handling
- to use badly written unstable garbage, and get away with it.
Personally, I genuinely prefer the messiness of error code handling, as it is easier to follow and easier to debug, and of course I worry that (most) people will think they are getting something which is actually really hard to do right, for free.
Pete