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 break and continue
Mar 21, 2023 programming

PHP break and continue

Today we are going to learn about break and continue in PHP. These two words are going to help you a lot when dealing working with loops. you can avoid a lot of extra iterations and save time. ...

5 Min Read Read More
PHP string functions
Mar 21, 2023 programming

PHP string functions

Today we are going to learn about some of the useful string functions in PHP. we are going to send a text through different functions and learn what those functions do. ...

9 Min Read Read More
Alpine.js Beginner to Advanced 2025: Everything You Need to Know
Jul 18, 2025 programming

Alpine.js Beginner to Advanced 2025: Everything You Need to Know

JavaScript frameworks have exploded in popularity over the past decade, but not every project needs—or benefits from—the complexity of tools like React or Vue. Sometimes, all you want is to sprinkle interactivity into your HTML without a heavy build process. This is exactly where Alpine.js shines. ...

29 Min Read Read More
Docker Beginner to Advanced 2025: The Complete Guide for Developers
Jul 18, 2025 programming

Docker Beginner to Advanced 2025: The Complete Guide for Developers

Containers have transformed the way we develop, deploy, and scale applications. Docker leads the charge, offering a developer-friendly platform to create consistent, portable environments from development to production. ...

27 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 ...