marcbraulio Posted February 27, 2012 Share Posted February 27, 2012 Why exactly was "try" and "catch" introduced into PHP? What are the benefits of using it over "if and else" and other similar methods? Any insight is much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/257851-why-was-try-and-catch-introduced-into-php/ Share on other sites More sharing options...
silkfire Posted February 27, 2012 Share Posted February 27, 2012 try / catch is a variant of the while loop. It means code execution should be broken if an Exception occurs. I guess it's more elegant to "take care" of the error instead of letting it occur by nature. Quote Link to comment https://forums.phpfreaks.com/topic/257851-why-was-try-and-catch-introduced-into-php/#findComment-1321642 Share on other sites More sharing options...
AyKay47 Posted February 27, 2012 Share Posted February 27, 2012 a try catch block is in no way shape or form a loop. It basically is an object oriented approach to error reporting, and really depends on your style of coding. this: $number = $_GET['number']; if($number == 0) { echo "number cannot be 0"; } else { //proceed with code } does the exact same thing as this: $number = $_GET['number']; try { if($number == 0) { throw new Exception("number cannot be 0"); } //code down here will only be executed if the exception is not thrown } catch(Exception $e) { echo $e->getMessage(); //outputs "number cannot be 0" } So, IMO if you are not programming in OO, you should stick to the if else blocks. It makes more sense to use OO error_reporting if you code in OO, and procedural if you code in procedural. But this is entirely up to you. I prefer to use try catch blocks simply because it allows me to check for multiple errors in one block and then proceed with the rest of the code, instead of having to use multiple elseif statements. Since a try block is broken out of after an Exception has been thrown and any code following the thrown Exception will not be executed, this allows for errors to be "tried" before the following code is executed. Quote Link to comment https://forums.phpfreaks.com/topic/257851-why-was-try-and-catch-introduced-into-php/#findComment-1321648 Share on other sites More sharing options...
ManiacDan Posted February 27, 2012 Share Posted February 27, 2012 Exception Handling is a completely different way of programming and handling errors. It's especially useful in Object Oriented programming. Quote Link to comment https://forums.phpfreaks.com/topic/257851-why-was-try-and-catch-introduced-into-php/#findComment-1321666 Share on other sites More sharing options...
marcbraulio Posted February 27, 2012 Author Share Posted February 27, 2012 Thank you all for the insight! Very useful. Also "AyKay47", thanks for the simple, yet in-depth example and explanation, made a lot of sense to me. Quote Link to comment https://forums.phpfreaks.com/topic/257851-why-was-try-and-catch-introduced-into-php/#findComment-1321733 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.