# How to use exceptions in PHP

> Learn how to use exceptions in PHP with try and catch blocks, inspect the error with getMessage, catch specific types, and run cleanup code in finally.

Author: [Flavio Copes](https://flaviocopes.com/about/) | Published: 2022-08-25 | Topics: [PHP](https://flaviocopes.com/tags/php/) | Canonical: https://flaviocopes.com/php-exceptions/

Sometimes errors are unavoidable.

Something completely unpredictable happens.

But many times, we can think ahead, and write code that can intercept an error, and do something sensible when this happens. Like showing a useful error message to the user, or try a workaround.

We do so using **exceptions**.

Exceptions are used to make us, developers, aware of a problem.

We wrap some code that can potentially raise an exception into a `try` block, and we have a `catch` block right after that. That catch block will be executed if there’s an exception in the try block:

```php
try {
  //do something
} catch (Throwable $e) {
  //we can do something here if an exception happens
}
```

Notice that we have an `Exception` object `$e` being passed to the `catch` block, and we can inspect that object to get more information about the exception, like this:

```php
try {
  //do something
} catch (Throwable $e) {
  echo $e->getMessage();
}
```

Let’s do an example.

For example by mistake I divide a number by zero:

```php
echo 1 / 0;
```

This will trigger a fatal error and the program is halted on that line:

![Fatal error message showing Uncaught DivisionByZeroError with stack trace when dividing by zero](https://flaviocopes.com/images/php-exceptions/Screen_Shot_2022-06-26_at_20.12.59.jpg)

Wrapping the operation in a try block and printing the error message in the catch block, the program ends successfully, telling me the problem:

```php
try {
  echo 1 / 0;
} catch (Throwable $e) {
  echo $e->getMessage();
}
```

![Browser output showing Division by zero error message caught and displayed by the try-catch block](https://flaviocopes.com/images/php-exceptions/Screen_Shot_2022-06-26_at_20.13.36.jpg)

Of course this is a simple example but you can see the benefit: I can intercept the issue.

Each exception has a different class. For example we can catch this as [`DivisionByZeroError`](https://www.php.net/manual/en/class.divisionbyzeroerror.php) and this lets me filter the possible problems and handle them differently.

I can have a catch-all for any throwable error at the end, like this:

```php
try {
  echo 1 / 0;
} catch (DivisionByZeroError $e) {
  echo 'Ooops I divided by zero!';
} catch (Throwable $e) {
  echo $e->getMessage();
}
```

![Browser output showing custom error message Ooops I divided by zero from specific DivisionByZeroError catch block](https://flaviocopes.com/images/php-exceptions/Screen_Shot_2022-06-26_at_20.15.47.jpg)

And I can also append a `finally {}` block at the end of this try/catch structure to execute some code after the code is either executed successfully without problems, or there was a _catch_:

```php
try {
  echo 1 / 0;
} catch (DivisionByZeroError $e) {
  echo 'Ooops I divided by zero!';
} catch (Throwable $e) {
  echo $e->getMessage();
} finally {
  echo ' ...done!';
}
```

![Browser output showing custom error message with finally block execution displaying done at the end](https://flaviocopes.com/images/php-exceptions/Screen_Shot_2022-06-26_at_20.17.33.jpg)

You can use the [built-in exceptions](https://www.php.net/manual/en/reserved.exceptions.php) provided by PHP but you can also create your own exceptions.
