Re: Try/Catch

new topic     » goto parent     » topic index » view thread      » older message » newer message
mattlewis said...
Spock said...
dcuny said...

.. For example, here's the "recovery" on an attempt to open a file that failed because of some exception:

... 
case OPEN_FILE then 
   try 
      open_file(fileName) 
   catch e 
      alert("The file " & fileName & " could not be opened. " & e[EXCEPTION_MESSAGE]) 
   end try 
... 

In this instance try/catch has become like a mere proxy for if/then. So my point is that status codes will do just as good a job (at this low level).

Not necessarily. We don't know what open_file() does. But let's assume for now that it's a library function that opens some file format and does some sort of processing on it to make it ready to use.

Perhaps you have a corrupt file in a way that the library doesn't detect. Now you have some kind of error happening inside the library. It's not something going on right there. Maybe it's a bad index, or subscripting an atom. But it's an error that happened as a result of calling open_file().

One of the nice things about try/catch is that it propagates errors back up the call stack, instead of requiring error handling / checking at each level.

Spock said...

Because you were referring to things like "[bad indexes are] the typical exception in Euphoria." this is why I was focusing on that low level aspect. For the high level this can still be done with SCC. Only that the traversal path must be manually coded. Still, no tricky stack unwinding is needed. Just return the functions via an error path. Granted. More verbose (what Jim calls 'ugly') but, I think, very robust.

I would say this is the opposite of robust. It requires the coder to exercise a lot of discipline and good practices. History shows us that this situation is all too rare. If we can come up with a way to make it all happen, but make it easier on the coder to do the right thing, that's what we should do. In this case, it seems clear that try/catch is better than any sort of error code scheme.

Matt

If the error code is ORed instead of MOVed then explicit handling is not required at each step. It will simply "bubble up" until it encounters a test written for it. Whole levels could be written without any error checking but the error code will still happily pass through.

Spock

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

Search



Quick Links

User menu

Not signed in.

Misc Menu