How to handle exceptions in PHP ? Last Updated : 03 Mar, 2022 Comments Improve Suggest changes Like Article Like Report Exceptions in PHP: The exception is the one that describes the error or unexpected behavior of the PHP script. The exception is thrown in many PHP tasks and classes. User-defined tasks and classes can also do differently. The exception is a good way to stop work when it comes to data that it can use. Throw Exceptions in PHP: The throw statement in PHP allows for a defined function or method to do otherwise. If alternatives are thrown, the following code will not be used. If the exception is not detected, a dangerous error will occur with the message "Uncaught Exception". Example 1: PHP <?php function division_operation($dividend, $divisor) { if($divisor == 0) { throw new Exception("Divide by Zero Error"); } return $dividend / $divisor; } echo division_operation(12, 0); ?> Output: Example 2: The following example demonstrates the use of the PHP try...catch statement to avoid the above scenario. PHP <?php function division_operation($dividend, $divisor) { if($divisor == 0) { throw new Exception("Raise Exception : Division by 0"); } return $dividend / $divisor; } try { echo division_operation(12, 0); } catch(Exception $e) { echo "Exception is Caught! : Unable to divide by 0"; } ?> Output: Exception is Caught! : Unable to divide by 0 Handling exceptions using try ... catch ... finally: Example 3: In the following code, whatever is present in the "finally" statement will be executed regardless of the exception. PHP <?php function division_operation($dividend, $divisor) { if($divisor == 0) { throw new Exception("Raise Exception : Division by 0"); } return $dividend / $divisor; } try { echo division_operation(12, 0); } catch(Exception $e) { echo "Exception is Caught! : Unable to divide by 0........"; } finally { echo "Finally block execution - Process complete."; } ?> Output: Exception is Caught! : Unable to divide by 0........Finally block execution - Process complete. Comment More infoAdvertise with us Next Article How to handle exceptions in PHP ? A Akash7 Follow Improve Article Tags : PHP Geeks-Premier-League-2022 PHP-Questions Similar Reads How to do exception handling in Ajax ? The purpose of this article is to demonstrate how we handle the exception in the jQuery AJAX request. A basic understanding of HTML, CSS, and jQuery is required. This can be done by an AJAX fail() method. We discuss 3 AJAX methods to better understand what's going on when making any ajax() request f 3 min read Servlet - Exception Handling When a servlet throws an exception, the web container looks for a match with the thrown exception type in web.xml configurations that employ the exception-type element. To define the invocation of servlets in response to particular errors or HTTP status codes, you'd have to utilize the error-page el 4 min read Exception Handling in Programming Exception handling is a critical aspect of programming, enabling developers to manage unexpected or erroneous situations gracefully. In this article, we'll discuss the concept of exception handling, its importance, and best practices for implementing it effectively in various programming languages. 7 min read Spring MVC - Exception Handling Prerequisites: Spring MVC When something goes wrong with your application, the server displays an exception page defining the type of exception, the server-generated exception page is not user-friendly. Spring MVC provides exception handling for your web application to make sure you are sending your 6 min read PHP restore_error_handler() Function The restore_error_handler() function is an inbuilt PHP function that facilitates restoring the previous version of an error handler function. Syntax: restore_error_handler(): boolParameter: This function does not accept any parameter. Return Value: This function will always return "true". Example 1: 1 min read Like