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 Interface
Mar 22, 2023 programming

PHP Interface

Today we are going to talk about interfaces in PHP. Interface is like a skeleton for the class. Interface is an important topic in OOP and it helps you manage your code better. ...

6 Min Read Read More
Transitioning from PHP to Python: A Comprehensive Guide for PHP Developers
Sep 10, 2024 programming

Transitioning from PHP to Python: A Comprehensive Guide for PHP Developers

While PHP and Python share several core programming concepts, transitioning between the two requires understanding the key differences in syntax, paradigms, and best practices. ...

19 Min Read Read More
What Modern PHP Looks Like in 2025
Jul 18, 2025 programming

What Modern PHP Looks Like in 2025

PHP has quietly evolved over the years, shedding many of its dated stereotypes while embracing modern programming practices and tooling. What used to be a language mocked for its inconsistencies and spaghetti-code reputation is now a mature, robust, and highly adaptable part of the web development ecosystem. ...

20 Min Read Read More
New PHP Tools You’ve Probably Never Heard Of
Jul 15, 2025 programming

New PHP Tools You’ve Probably Never Heard Of

The PHP ecosystem is constantly evolving, with fresh packages, libraries, and tools emerging that aim to solve old problems in new ways. There’s a growing landscape of lesser-known tools quietly gaining traction within the PHP community. ...

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