Free cookie consent management tool by TermsFeed Generator PHP catch exceptions | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP catch exceptions

PHP catch exceptions

Last Updated on Feb 14, 2023

Most of the time in your script you might face some exceptions, especially when you are handling unknown files or user inputs.

So today we are going to learn how to catch those exceptions.

In PHP the structure for catching exception is like this:

try {
  // your code here
} catch(Exception $e) {
  // what to do if there was an exception
}

We write the code in the try part. And in the catch part we tell php what to do if there was an exception

Catch accepts the type of exception you want to catch. In the above example we use Exception to accept all the exceptions and we tell PHP to store the exception (whatever it was) in the $e variable. (or you can name it whatever you want)

You can catch multiple exceptions by using multiple catches

For example

try {
   // your code here
} catch(Exception $e) {
   // what to do if there was an exception
} catch(ArithmeticError $e) {
   // what to do if there was an arithmetic error
}

Or you can use throwable to catch everything (all the exceptions and errors)

try {
  // your code here
} catch(Throwable $th) {
   // what to do if there was an exception or an error
}

Now let’s see an example

try {
   $x = 10;
   $x->getName();
} catch (Throwable $e) {
   echo 'x is not an object';
}

We can throw an exception ourselves by doing this

throw new Exception(message,code,previous)

Message: is the error message
Code: is the error code
Previous: is the previous throwable, you can use this is you want to chain the errors together

In the catch part we tell php to store the error or exception in the $e variable. The $e variable is an object and we can get very useful information from it, like the file or line that causes the error, error message and error code

Let’s see an example

try {
    $x = 10;
    if ($x > 7) {
        throw new Exception("x is too big", 70);
    }
} catch (Throwable $th) {
    $th->getLine();
    $th->getMessage();
    $th->getCode();
    $th->getFile();
}
// 5
// x is too big
// 70
// C:\xampp\htdocs\examples\index.php

Handling the errors and exceptions that might be thrown during your code is very important. And now you know how to do it. Isn’t it great?

Conclusion

Now you know about handling exceptions and errors in PHP.

I recommend you to open a PHP files and try to catch different types of errors and exceptions. try to throw exceptions and write your own message.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • try catch
  • catch exceptions
  • catch errors
  • throw exceptions
  • throwable

Category: programming

Tags: #php

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

PHP array map
Feb 14, 2023 programming

PHP array map

Today we are going to learn about array map function in PHP. It lets you apply a function to the elements of the given arrays and returns an array with changed elements. ...

4 Min Read Read More
PHP array filter
Feb 14, 2023 programming

PHP array filter

Today we are going to talk about array filter in PHP. array filter is a very useful function and it helps you filter an array by key, value or both. ...

6 Min Read Read More
PHP calculate distance between 2 places in 9 easy steps
Feb 14, 2023 programming

PHP calculate distance between 2 places in 9 easy steps

Today we are going to talk about calculating the distance between two place in PHP in a few easy steps. ...

7 Min Read Read More
React Beginner to Advance 2025: A Complete Guide to Building Modern Web Applications
Jul 18, 2025 programming

React Beginner to Advance 2025: A Complete Guide to Building Modern Web Applications

React JS has firmly established itself as one of the most widely adopted JavaScript libraries for building dynamic user interfaces. Whether you're crafting a small personal website or architecting a large-scale enterprise dashboard, React's component-based architecture and flexible ecosystem allow developers to create highly performant, scalable, and maintainable applications. ...

29 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...