PHP Exceptions: Try Catch for Error Handling | How To

In this post, you’ll learn how to use exception handling in PHP. As of PHP 5, we can use try catch blocks for handling—this is a better way to handle exceptions and control the flow of your application. In this article, we’ll go through the basics of exception handling along with a couple of real-world examples.

What is an Exception?

PHP 5 introduced a new error model which allows you to throw and catch exceptions in your application—this is a better way of handling errors than what we had in older versions of PHP. All exceptions are instances of the base class Exception, which we can extend to introduce our own custom exceptions.

It’s important to note here that exception handling is different than error handling. In error handling, we can use the set_error_handler function to set our custom error handling function so that whenever an error is triggered, it calls our custom error handling function. In that way, you can control errors. Generally however, certain kinds of errors are unrecoverable and stop program execution.

On the other hand, exceptions are something that are thrown deliberately by the code, and it’s expected that it’ll be caught at some point in your application. So we can say that exceptions are recoverable as opposed to certain errors which are unrecoverable. If an exception which is thrown is caught somewhere in your application, program execution continues from the point where the exception was caught. And an exception which is not caught anywhere in your application results in an error, thus halting program execution.

Exception Control Flow

Let’s refer to the following diagram that shows the generic exception handling control flow.

Exception handling in a try catch finally block

can be thrown and caught by using the try and catch blocks. You are responsible for throwing exceptions when something occurs which is not expected. Let’s quickly go through the basic exception handling flow as shown in the following pseudo code.

Most of the time, when you’re dealing with exceptions, you’ll end up using a pattern as shown in the above snippet. You can also use the finally block along with the try and catch blocks, but we’ll get back to that later in this article.

The try block is the one to use where you suspect that your code may generate an exception. You should always wrap such code using try and catch.

Throwing an Exception

An exception might be thrown by a function that you call, or you can  use the throw keyword to throw an exception manually. For example, you might validate some input before performing any operation, and throw an exception if the data is not valid.

It’s important to note here that if you throw an exception but you haven’t defined the catch block which is supposed to handle that exception, it’ll result in the fatal error. So you need to make sure that you always define the catch block if you’re throwing exceptions in your application.

Once an exception is caught in the catch block, the Exception object contains the error message which was thrown using the throw keyword. The $e variable in the above example is an instance of the Exception class, so it has access to all methods of that class. In this block, you should define your own exception handling logicwhat exactly you want to do with the error you catch.

In the next section, we’ll go through a real-world example to understand how exception handling works.

A Real-World Example

In this section, we’ll build a real-world example to demonstrate exception handling in PHP.

Let’s assume that you’ve built an application which loads application configuration from the config. file. Now, it’s essential that the config.php file must be present when your application is bootstrapped. Thus, your application can’t run if the config.php file is not present. So this is the perfect use-case to throw an exception and let the user know they need to fix the issue.

As you can see in the above example, we’re checking for the existence of the config.php file in the beginning of the bootstrapping phase. If the config.php file is found, the execution is continued normally. On the other hand, we’ll throw an exception if the config.php file doesn’t exist. Also, we would like to stop execution as well in case there’s an exception!

So that’s how you can use exceptions in your applications. You should throw exceptions for use-cases that are exceptional—you shouldn’t unnecessarily throw exceptions for generic errors like invalid user credentials, improper directory permissions etc., that you expect to happen frequently. These are better handled by generic error messages in the regular application execution flow.

So that was an example of handling exceptions using the default Exception class. In the next section, we’ll see how you can extend the core Exception class and create your own custom exceptions in your application.

How to Create Custom Exceptions

In this section, we’ll discuss how you can create custom exceptions in your applications. In fact, we’ll extend the example which we’ve just discussed in the previous section to demonstrate custom exceptions.

In the previous example, we threw the configuration exception using the default Exception class. That’s perfectly fine as long as you just want to deal with the exception error message. However, sometimes you want to do a bit more based on the type of exception which is being thrown. That’s where custom exceptions are useful.

Let’s revisit the previous example as shown in the following snippet.

You might also like More from author

Leave A Reply

Your email address will not be published.