PHP Exceptions: Try Catch for Error Handling | How To

0
Want create site? Find Free WordPress Themes and plugins.

In this post, you’ll learn how to use exception in . As of 5, we can use try blocks for handling—this is a better way to handle 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 Handling 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

Exceptions 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.php 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.

Did you find apk for android? You can find new Free Android Games and apps.

You might also like More from author

Leave A Reply

Your email address will not be published.