Exception handling is used to change the normal flow of the code execution if a specified error (exceptional) condition occurs. This condition is called an exception.
An exception can be thrown, and caught (“catched”) within PHP. Code may be surrounded in a try block, to facilitate the catching of potential exceptions. Each try must have at least one corresponding catch block.
Basic use of Exceptions
Creating a custom exception handler
Re-throwing an exception
Setting a top level exception handler
Code may be surrounded in a try block, to help catch potential exceptions
Each try block or “throw” must have at least one corresponding catch block
Multiple catch blocks can be used to catch different classes of exceptions
Exceptions can be thrown (or re-thrown) in a catch block within a try block
// do something that can go wrong
catch (Exception $e)
throw new Exception( 'Something really gone wrong', 0, $e);
//create function with an exception
throw new Exception(â€œValue must be 1 or below);
//trigger exception in a array block
//If the exception is thrown, this text will not be shown
echo If you see this, the number is 1 or below;
echo "Message: " .$e->getMessage();
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.
Article Tags: catch exception in php, catch exception php, exception handling in php, exception handling php, how to catch error in php, how to handle exception in php, Jquery, PHP, php error handling try catch, php exception handling, php exception handling example, php handle exception