Working in some legacy code, I've run across a ton of Try/Catch statements. Try/Catch isn't something that they taught in my Zend certification classes, and in 10 years I've not worked with another PHP developer who's used it. Does Try/Catch have extra overhead compared to doing if statements? What would make it more or less desirable than other options?
-
11Figures a course on PHP didn't teach about error handling. – cHao Aug 06 '10 at 16:50
-
Are the try/catch blocks used for error handling or for ordinary flow control? – Remus Rusanu Aug 06 '10 at 16:51
-
possible duplicate of [Performance of try-catch in php](http://stackoverflow.com/questions/104329/performance-of-try-catch-in-php) – Gordon Aug 06 '10 at 16:52
-
3@cHao He probably was taught how to use error handling without exceptions as was the standart before php 5. I bet your language supported EVERYTHING when it was created too. – Iznogood Aug 06 '10 at 16:52
-
*(related)* http://stackoverflow.com/questions/651619/what-is-the-advantage-of-using-try-catch-versus-if-else – Gordon Aug 06 '10 at 16:53
-
1@Remus Rusanu: Error handling ONLY. If i ever catch you trying to use them for ordinary flow control, i will revoke your computer privileges. – cHao Aug 06 '10 at 16:53
-
3In 10 years you've never worked with someone who knows what they're doing? – ryeguy Aug 06 '10 at 16:54
-
@cHao: Can you revoke the computer privileges of some of the developers (new and old) on my team? I've come across this too many times :) – David Aug 06 '10 at 16:54
-
@Iznogood: my languages (C++, VB.net, C#, and even Perl) have had some form of exception handling since way before i ever first used them. – cHao Aug 06 '10 at 16:55
-
@cHao: I'm not asking a theory question, I'm asking the OP how is exception handling used *in his project* – Remus Rusanu Aug 06 '10 at 16:55
-
Was your certification class for php4? Exceptions were introduced in 5, and if your cert is in php5 and they didn't teach exceptions, then the cert is even more worthless than I originally thought (that's not saying much)... – ircmaxell Aug 06 '10 at 16:57
-
@cHao How about C? Want us to rename php5 as php++ so you "get" the difference? – Iznogood Aug 06 '10 at 16:58
-
@Remus Rusanu: Ahh. I'm going to assume it was used correctly there. It'd be too much work hunting down the original authors to yank their power cables. – cHao Aug 06 '10 at 16:59
-
@Iznogood: Psh. C++ is not C. It's a whole other language that just happens to be quite backward-compatible. – cHao Aug 06 '10 at 17:00
-
@cHao Well such difference exists between php4 and php5. – Iznogood Aug 06 '10 at 17:01
-
@Iznogood: PHP5 has been out for...let's see....6 years now, and has been the ONLY supported version of PHP for at least 2 -- and people still aren't learning how to use the stuff that's been built into it since its release? – cHao Aug 06 '10 at 17:08
-
@cHao How is that the fault of the language? I did C++ for 8 years before php and let me tell you there a TON of stuff in there peoples should use but don't. Anyways. Did'nt want to start a religion war. – Iznogood Aug 06 '10 at 17:10
-
@Iznogood: Oh, i don't blame the language. I blame the people who taught "programmers" who look at a construct that's been part of damn near every modern language (and some ancient ones) -- *even their own* --for years and scratch their heads wondering what it does. – cHao Aug 06 '10 at 17:17
-
@cHao Well then we finally agree. The problem is not the language but the teachers! ;-) – Iznogood Aug 06 '10 at 17:21
-
Wow, I did start a war! Indeed, my cert was in PHP4 and unfortunately my head's been buried in Java and ZF UI type work for the last couple of years, so I've not had to work on this side of the process for quite a while. Yes, there are cases in the code where it has been used for flow control (branching logic) rather than error handling. Hence, the reason we're trying to clean it up. It does appear, though, that they did use it correctly in some scenarios. – bpeterson76 Aug 06 '10 at 17:45
-
3@bpeterson76: Umm, if you've worked in Java, exceptions are way more prominent there, you couldn't have missed them; and the meaning is the same. – Amadan Aug 06 '10 at 17:52
5 Answers
I don't consider them to really be related to each other.
If statements are for determining branching logic.
Try/Catch is to handle errors that occur. Exceptions that would halt the program can be handled in the Catch block.

- 38,138
- 7
- 87
- 101
Well, if I understand correctly, a try/catch block adds a layer to the stack. So yes, there can be significant performance issues with it. However, the gains that it provides by letting you handle errors where you need to are significant as well. An if
statement has very little overhead. So to directly answer your question, yes try/catch has a significantly higher overhead than if/then (Throwing exceptions has a LOT more overhead, since it generates a backtrace for each throw).
With that said, they both have their purpose. Exceptions should be used for, well, exceptional conditions. You should use them to detect things that went wrong that are not within the normal realm of failure. For example, you wouldn't throw an exception if a user didn't enter a long enough password on the registration page. But you would throw an exception if you couldn't connect to the database to perform the registration. One is a logic error, and the other is a condition that needs to interrupt normal program flow.

- 163,128
- 34
- 264
- 314
Try/catch is used for error handling. If statements are simple boolean testers. They don't do the same things at all. You should use if statements and test for each condition you know about, but use try/catch for exception handling.

- 159,648
- 54
- 349
- 530
The whole point of try/catch
is that it is non-local. You can exit multiple loops at a stroke, break out of nested function calls, escape from anywhere you get into. if
can't do that, and is not meant to. I do not know about the overhead, but I strongly and informedly suspect that it has much more than if. Ultimately, use the tool right for the job: they are not interchangeable.
Okay, they are, but they shouldn't be interchanged :)
UPDATE: Many other people say that try/catch
are for error handling. They are not. They are for exception handling. In many languages, for example, trying to get a next element from the iterator on its last element will raise an exception; this is a perfectly valid use of exceptions. You can use them whenever something unexpected happens, which has to be handled outside the current scope (assuming you are not providing a callback to handle it).

- 191,408
- 23
- 240
- 301
Of course it does. But the gains from making error handling that much easier are worth it.

- 776,304
- 153
- 1,341
- 1,358