From: Brian H. <bh...@sp...> - 2004-06-29 03:04:10
|
On 29 Jun 2004, skaller wrote: > In general, I think exceptions are a bad idea. > I spend a lot of time removing them from my code :( Looking at your examples, I think you're trying to fix something that doesn't need fixing. My general rule is "don't catch exceptions that you don't know how to handle". The default handling of uncaught exceptions isn't perfect, but it's orders of magnitude better than most program's error handling, and I can't think of a better way to handle things. Or, to put it a different way, exceptions are the worst possible way to handle errors, except for all the other ways. Certainly, quietly discarding exceptions is a real bad idea. Code like: try ... with | _ -> () is evil. Dropping certain exceptions, yes. But all exceptions? As for the theoretical foundations, I wouldn't be surprised if someone who follows the theory comes back with actual citations. I don't think the theoretical foundations would be that hard- exceptions are just an alternate return path. Actually, I go all types of expressions can be one of two things- either an exception or the unexceptional type. Then I start defining my basic operators to properly "pass along" an exception. So, for example, an if statement of the form "if expr1 then expr2 else expr3" would be evaluated like this: - if expr1 evaluates to an exception e, the entire expression evaluates to that exception - if expr1 evaluates to true, the expression evaluates to whatever expr2 evaluates to (this can be an exception) - if expr1 evaluates to false, the expression evaluates to whatever expr3 evaluates to. This keeps me firmly within strongly typed territory, as I understand things. Compared to monads, this is pretty trivial. -- "Usenet is like a herd of performing elephants with diarrhea -- massive, difficult to redirect, awe-inspiring, entertaining, and a source of mind-boggling amounts of excrement when you least expect it." - Gene Spafford Brian |