PHP Exceptions: Try & Catch.

This is a beginners tutorial on how to use try and catch blocks in PHP. In this tutorial, I will show you how to throw exceptions and “catch” them using PHP.

Lets take a look at a simple example of an exception being thrown (and subsequently caught) in PHP:

In the above code snippet, I’ve created a TRY CATCH example where an exception is always thrown (obviously, I am only doing this for example purposes):

  1. Inside the TRY block, we attempt to carry out some sort of operation. In this case, we are checking to see if the number one is equal to the number two. Because one is not equal to two (and it never will be), we throw an Exception with the message “1 is not equal to 2!”.
  2. Inside our CATCH block, we catch the Exception and print out the Exception message.

To sum it up:

  • TRY: Inside the TRY block, we carry out our application logic. This TRY block contains code that may or may not throw an Exception.
  • CATCH: The CATCH block will “catch” any exceptions that occurred inside the preceding TRY block. The code inside our CATCH block will only be executed if an Exception occurs.
  • FINALLY: If you’re using PHP 5.5 or above, you may want to use a FINALLY block. A FINALLY block will always be executed, regardless of whether an Exception occurred or not.

When to use exceptions?

Typically, you should use exceptions whenever the result of an operation is different from what your application expects. For example, if your application attempts to read a CSV file on the server, but that file does not exist, then you could throw an exception like so:

In the example above, we throw an Exception if we fail to open the file in question. We throw an Exception because the file in question should exist. Examples of when you could throw an Exception:

  1. Your PHP application fails to connect to MySQL.
  2. Your database query fails.
  3. An API call fails.
  4. The incorrect request type is received.
  5. A required $_POST or $_GET variable is missing.

Basically, if the unexpected happens, then you should probably throw an exception, especially if the failure / error in question impacts the logic of your application.

Should I catch all exceptions?

Personally, I don’t think so. For example: If you fail to connect to your database and an exception is thrown, do you really want the rest of your code to execute? If the rest of your code is reliant on a database connection that doesn’t exist, should that code be executed?

In my opinion, exceptions should only be caught if the exception in question doesn’t adversely affect the rest of your application. i.e. Your code isn’t reliant on the operation that caused the Exception.

For example: If an API call to an external service fails, you might want to catch the exception and display a user-friendly error message such as “Could not connect to service” or “Weather information unavailable” (if you are connecting to a weather API).

Uncaught exceptions should be managed via a custom exception handler. That way, you can handle the uncaught exception and display a user-friendly message / splash screen.