PHP Tutorial

A web server programming language.

PHP Exceptions

PHP Exceptions

An exception is an object that describes an error or unexpected behaviour of a PHP script.

The try...catch Statement

To avoid the error from being thrown and stopping script execution, we can use a try...catch block to catch the exception and run some code instead.

  • try: A block of code in which exceptions can be thrown
  • catch: A block of code that will be executed if a particular exception is thrown
  • throw: Allows you to trigger an exception

Try It Yourself

Edit the code below and click Run to see the result live.

Source Code
Result
Topics