Re: Try/Catch

new topic     » goto parent     » topic index » view thread      » older message » newer message

Here's a proposal for something more "Euphoria-like", that might address some of the perceived abuses of try/catch. It's modeled roughly off the pcode() ("protected code") call in Lua.

First, the general form, for a procedure:

-- do something dangerous with a function 
if protected_procedure( scary_procedure(a, b, c) ) then 
   -- handle the exception 
end if 

For those paying close attention, this requires some serious gymnastics on the part of the parser. That's because normally, the argument list is processed before the call to the routine. I'll hand-wave this away for the moment, and get on to the next case: calling a function.

The call to pcode() in Lua looks something like this:

error, theta = pcode( sin(x) ) 

This doesn't work well in Euphoria because Euphoria doesn't know if a sequence is coming back as a single value from the function, or a list of parameters from the function.

And there's a subtle point here: assignment should be conditional. If the call to the function resulted in an error, the assignment shouldn't take place, because the result (if any) is likely to be bad.

I think we can kill two birds with one stone here:

-- do something dangerous with a procedure 
if protected_function( {x, y}, scary_function(a, b, c) ) then -- safe version of {x, y} = scary_code(a, b, c) 
   -- handle the exception 
end if 

Where the actual assignment of:

{x, y} = scary_code(a, b, c) 

would only take place if scary_code didn't raise an error.

Behind the scenes, it might generate code that's roughly:

-- set the protected_call flag for the frame 
set_protected_call(1) 
 
-- perform protected code 
__PROTECTED_RESULT = scary_function(a, b, c) 
 
-- clear the protected_call flag for the frame 
set_protected_call(0) 
 
-- assignment is conditional 
if not __ERROR_FLAG then 
   {x, y} = __PROTECTED_RESULT 
end if 
 
-- deref the returned value from the temp 
__PROTECTED_RESULT = 0 
 
-- the user's custom code 
if __ERROR_FLAG then 
   -- handle the exception 
end if 

So when RTFatal executes, it unwinds the call stack until if finds a call frame where the protected_call flag is true.

I won't even suggest how this might work in C.

Is this sufficiently Euphoria-like?

- David

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu