In PHP, Handling errors with try catch blocks is almost the same as handling errors in other programming languages. PHP runtime looks for a catch statement that can handle that type of exception when a PHP exception is thrown. Until a catch statement is found, It checks for the calling methods up to the stack trace. In this article we will explore Exception Handling In PHP. Following pointers will be covered in this article,
Exception Handling In PHP
The exception is handed to the global exception handler If one is not found. For example, if you had some sort of error on your website, you would be in a position where you turn on display errors for your site so that those errors that show up and user would be able to see those errors and that would have an advantage in a sense that when they see them, they would be likely to contact you in order to inform about error. However, the downside of that is that they can make you look like you don’t know what you are doing and ruin the user experience.
The other option that you have is to suppress those errors and what often happens is that user will get a white screen of death and it won’t show any error because you are suppressing them and now, the user cannot see this big ugly error and they don’t know what is happening. So if they contact you, they can just say that it is a white screen and it is harder for you to debug.
Moving on with this article on Exception Handling In PHP,
Try, Catch, Throw And Finally
Try catch blocks allow us to get both worlds. They allow us to hide those ugly errors from our users but at the same time, catch those errors and be able to do something with them.
Some specialized keywords are provided for this purpose.
- try: Block of code is represented in which exception can arise. The code will continue as normal if the exception does not trigger.
- catch: Block of code is represented that will be executed when a particular exception has been thrown.
- throw: In order to throw an exception, we use throw. Each “throw” must have at least one “catch”. It is also used to list the exceptions that a function throws, but doesn’t handle itself.
- finally: It is put for cleanup activity in PHP code. Basically, It is used in place of catch block or after catch block.
Moving on with this article on Exception Handling In PHP. Lets walk through a quick example
Sample Program
<?php try { echo "this is try block.<br>"; // Try to run your code here } catch (exception $e) { echo "something went wrong"; //code to handle the exception } finally { echo "this part is always executed"; //optional code that always runs } ?>
Sample Program
Below code demonstrates the flow of try catch block in PHP
<?php function example($var) { echo " Before try block.<br>"; try { echo "n Inside try block.<br>"; // Only if part will be executed, If var is zero if($var == 0) { //Only exception is thrown, If var is zero throw new Exception('Number is zero.<br>'); // This line will never be executed echo "n After throw "; } } // When Exception has been thrown by try block, Catch block will be executed catch(Exception $e) { echo "n Exception Caught.<br>", $e->getMessage(); } //This line will allways get executed echo "n After catch .<br>"; } // Exception will not be rised example(5); // Exception will be rised here example(0); ?>
With this we come to the end of this article on Exception Handling . I hope you have learned about handling errors with try catch blocks by going through few examples.
If you found this PHP Tutorial blog relevant, check out the PHP Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.
Got a question for us? Please mention it in the comments section of this article and I will get back to you.