Comments on: Stop Catching Exceptions! http://gen5.info/q/2008/07/31/stop-catching-exceptions/ Towards Intelligent Systems Wed, 11 Jul 2012 12:59:08 +0000 hourly 1 http://wordpress.org/?v=3.0.3 By: Kyle Lahnakoski http://gen5.info/q/2008/07/31/stop-catching-exceptions/comment-page-1/#comment-5273 Kyle Lahnakoski Tue, 10 Mar 2009 01:31:12 +0000 http://gen5.info/q/?p=43#comment-5273 I posted a detailed argument for my position. In the process I now see the holes in my argument agaist finally. I thank you for that. I posted a detailed argument for my position. In the process I now see the holes in my argument agaist finally. I thank you for that.

]]>
By: paul_houle http://gen5.info/q/2008/07/31/stop-catching-exceptions/comment-page-1/#comment-4917 paul_houle Sat, 28 Feb 2009 04:28:01 +0000 http://gen5.info/q/?p=43#comment-4917 To some extent it's an unsolvable problem. You're not just concerned with the state of your program, you're also concerned with the state of all of the resources that it depends on -- all of which depend on objects in the physical universe that have less than 100% reliability. The real cause of an exception could be that you've got a bit that's stuck in the 1 position somewhere in your CPU and you're just screwed. I'm looking forward to what you write, but I think overall we need to balance between: (i) having a system that's easy to implement that does the right thing almost all of the time, or (ii) having a system that's hard to implement that would do the right thing all the time but that practically screws up more than option (i) because people keep dropping parts on the floor. To some extent it's an unsolvable problem. You're not just concerned with the state of your program, you're also concerned with the state of all of the resources that it depends on — all of which depend on objects in the physical universe that have less than 100% reliability. The real cause of an exception could be that you've got a bit that's stuck in the 1 position somewhere in your CPU and you're just screwed.

I'm looking forward to what you write, but I think overall we need to balance between: (i) having a system that's easy to implement that does the right thing almost all of the time, or (ii) having a system that's hard to implement that would do the right thing all the time but that practically screws up more than option (i) because people keep dropping parts on the floor.

]]>
By: Kyle Lahnakoski http://gen5.info/q/2008/07/31/stop-catching-exceptions/comment-page-1/#comment-4914 Kyle Lahnakoski Sat, 28 Feb 2009 03:48:19 +0000 http://gen5.info/q/?p=43#comment-4914 Finally? Really? With multiple entry points to the finally clause, you can not reasonably expect to know what state your program is in when you execute. I suggest you stick with the catch clause despite the extra verbosity; It will reveal scenarios you have missed. But thank you. Between your post, it's comments, and comments on YCombinator, I must post myself on how to deal with exceptions properly: Without loosing original trace paths, without cluttering the throws clause, and without creating non-deterministic code from finally clauses. Finally? Really? With multiple entry points to the finally clause, you can not reasonably expect to know what state your program is in when you execute. I suggest you stick with the catch clause despite the extra verbosity; It will reveal scenarios you have missed.

But thank you. Between your post, it's comments, and comments on YCombinator, I must post myself on how to deal with exceptions properly: Without loosing original trace paths, without cluttering the throws clause, and without creating non-deterministic code from finally clauses.

]]>
By: dkh http://gen5.info/q/2008/07/31/stop-catching-exceptions/comment-page-1/#comment-4883 dkh Fri, 27 Feb 2009 18:25:55 +0000 http://gen5.info/q/?p=43#comment-4883 There's no reason exceptions should only be used for exceptional cases. However, there's an important reason NOT to make this distinction: often the programmer doesn't know which cases are exceptional or not without knowing the complete context in which his code is used. In other words, you can't write reusable code that respects the "exceptions are exceptional" rule. Exceptions are a language feature and should be used whenever they are linguistically convenient; i.e., wherever they result in cleaner, more robust code than if you had used a different construct. In many cases they're very convenient for error handling. Occasionally they're useful for (hopefully clearly documented) flow control. You only reason to worry about how common a case is is when you're trying to figure out if throwing and catching exceptions will cause performance problems. There's no reason exceptions should only be used for exceptional cases. However, there's an important reason NOT to make this distinction: often the programmer doesn't know which cases are exceptional or not without knowing the complete context in which his code is used. In other words, you can't write reusable code that respects the "exceptions are exceptional" rule.

Exceptions are a language feature and should be used whenever they are linguistically convenient; i.e., wherever they result in cleaner, more robust code than if you had used a different construct. In many cases they're very convenient for error handling. Occasionally they're useful for (hopefully clearly documented) flow control. You only reason to worry about how common a case is is when you're trying to figure out if throwing and catching exceptions will cause performance problems.

]]>
By: paul_houle http://gen5.info/q/2008/07/31/stop-catching-exceptions/comment-page-1/#comment-4880 paul_houle Fri, 27 Feb 2009 17:38:50 +0000 http://gen5.info/q/?p=43#comment-4880 Well, programming culture and programming languages are something that we adapt to. The (approximate) exception handling model used in Java is widespread in mainstream languages (C#, Python, PHP) and is also seen in emerging languages such as Scala, oCaml, and F#. Restartable conditions, as seen in some Lisp implementations, have advantages, but one has to weigh them against the advantages of today's mainstream and emerging languages -- plus the switching cost. For instance, you could make a case that Python and Ruby are better than PHP (say 20%) but I'd need a language that would be much better than PHP to be worth the considerable cost involved in switching. Well, programming culture and programming languages are something that we adapt to.

The (approximate) exception handling model used in Java is widespread in mainstream languages (C#, Python, PHP) and is also seen in emerging languages such as Scala, oCaml, and F#. Restartable conditions, as seen in some Lisp implementations, have advantages, but one has to weigh them against the advantages of today's mainstream and emerging languages — plus the switching cost. For instance, you could make a case that Python and Ruby are better than PHP (say 20%) but I'd need a language that would be much better than PHP to be worth the considerable cost involved in switching.

]]>
By: Ken http://gen5.info/q/2008/07/31/stop-catching-exceptions/comment-page-1/#comment-4879 Ken Fri, 27 Feb 2009 16:48:38 +0000 http://gen5.info/q/?p=43#comment-4879 "Depending on the culture of the shop, he might be able to do it himself, he might talk about it with the other programmers over IM, he might need to get the signatures of three managers, or he might need to wait until the next group meeting." That sounds like a social issue, not a technical one. There will always exist social problems that cannot be solved by technical solutions. Even if you didn't have to declare any exceptions, the correct fix at one level might involve changing interfaces at other levels. And while I agree checked exceptions cause far more trouble than they're worth, isn't that a drop in the bucket compared to Java's lack of support for restartable conditions? You can say 2 words ("throws Exception") per method to basically escape the checked exception system, but if you want to fake restartable conditions it takes a ton of code every time (which Java doesn't offer the power to abstract). Whenever I've worked on large Java programs, "throw" hurts me far more than "throws". "Depending on the culture of the shop, he might be able to do it himself, he might talk about it with the other programmers over IM, he might need to get the signatures of three managers, or he might need to wait until the next group meeting."

That sounds like a social issue, not a technical one. There will always exist social problems that cannot be solved by technical solutions. Even if you didn't have to declare any exceptions, the correct fix at one level might involve changing interfaces at other levels.

And while I agree checked exceptions cause far more trouble than they're worth, isn't that a drop in the bucket compared to Java's lack of support for restartable conditions? You can say 2 words ("throws Exception") per method to basically escape the checked exception system, but if you want to fake restartable conditions it takes a ton of code every time (which Java doesn't offer the power to abstract). Whenever I've worked on large Java programs, "throw" hurts me far more than "throws".

]]>
By: paul_houle http://gen5.info/q/2008/07/31/stop-catching-exceptions/comment-page-1/#comment-4878 paul_houle Fri, 27 Feb 2009 15:05:53 +0000 http://gen5.info/q/?p=43#comment-4878 You're right -- thanks! I took the liberty of fixing it. You're right — thanks! I took the liberty of fixing it.

]]>
By: unwind http://gen5.info/q/2008/07/31/stop-catching-exceptions/comment-page-1/#comment-4876 unwind Fri, 27 Feb 2009 14:47:39 +0000 http://gen5.info/q/?p=43#comment-4876 Your point four (4) needs to be rewritten, as it stands it's a bit broken grammatically, methinks. Your point four (4) needs to be rewritten, as it stands it's a bit broken grammatically, methinks.

]]>
By: brian http://gen5.info/q/2008/07/31/stop-catching-exceptions/comment-page-1/#comment-4850 brian Fri, 27 Feb 2009 02:10:21 +0000 http://gen5.info/q/?p=43#comment-4850 I agree with the previous post that this isn't the right way to do it. But, even so, you should pretty much never catch "Exception." I'm not a C# programmer, but in Java you seem to think that checked exceptions are all that exist. There are runtime exceptions, and runtime exceptions do not need to be caught. IllegalArgumentException in Java is in fact a runtime exception. Runtime exceptions represent programming errors, not special conditions to be checked for. If you called the method incorrectly it's *your* fault not the users fault. A SQLException is a special condition that can be checked for (it happens often enough that the DB just aint there) ... and so Exceptions like SQLException must be specifically caught or rethrown to the calling code. The system shouldn't crash because the DB wasn't there. If you catch your SQLException with "catch ( Exception sqle)" ... then you risk catching runtime exceptions that should not be caught there. Exceptions that typically get thrown inherit from other exceptions. There could be a hundred sub-exceptions of illegal argument exceptions that could be thrown in Java (and a method signature might easily declare 5 of them), but I can easily enough catch all of them with one "catch(IllegalArgumentException ile)" line. I don't *have* to break it all up into each and every possible subclass. I agree with the previous post that this isn't the right way to do it.

But, even so, you should pretty much never catch "Exception."

I'm not a C# programmer, but in Java you seem to think that checked exceptions are all that exist. There are runtime exceptions, and runtime exceptions do not need to be caught. IllegalArgumentException in Java is in fact a runtime exception. Runtime exceptions represent programming errors, not special conditions to be checked for. If you called the method incorrectly it's *your* fault not the users fault.

A SQLException is a special condition that can be checked for (it happens often enough that the DB just aint there) … and so Exceptions like SQLException must be specifically caught or rethrown to the calling code. The system shouldn't crash because the DB wasn't there. If you catch your SQLException with "catch ( Exception sqle)" … then you risk catching runtime exceptions that should not be caught there.

Exceptions that typically get thrown inherit from other exceptions. There could be a hundred sub-exceptions of illegal argument exceptions that could be thrown in Java (and a method signature might easily declare 5 of them), but I can easily enough catch all of them with one "catch(IllegalArgumentException ile)" line. I don't *have* to break it all up into each and every possible subclass.

]]>
By: Generation 5 » What do you do when you’ve caught an exception? http://gen5.info/q/2008/07/31/stop-catching-exceptions/comment-page-1/#comment-723 Generation 5 » What do you do when you’ve caught an exception? Wed, 27 Aug 2008 15:19:06 +0000 http://gen5.info/q/?p=43#comment-723 [...] For the most part, the code inside DoUnitOfWork() and the functions it calls tries to throw exceptions upward rather than catch them. [...] [...] For the most part, the code inside DoUnitOfWork() and the functions it calls tries to throw exceptions upward rather than catch them. [...]

]]>

Warning: fsockopen(): php_network_getaddresses: getaddrinfo failed: No address associated with hostname in /hosting/sites/gen5.info/htdocs/q/wp-includes/class-snoopy.php on line 1148

Warning: fsockopen(): unable to connect to :80 (php_network_getaddresses: getaddrinfo failed: No address associated with hostname) in /hosting/sites/gen5.info/htdocs/q/wp-includes/class-snoopy.php on line 1148