And how to throw exceptions with custom messages. You don't have to use "dd()" anymore, become a pro.

This is a shot of the owner of New Zealand watch company - Hunters Race.
Photo by Hunters Race / Unsplash


When writing Laravel application, there are multiple points at which the application could fail. These points cause errors, and if the errors are not handled properly they will cause your application to completely breakdown. Which is an outcome we do not really desire. Application breakdowns will cause users to immediately lose interest in your application and reduce the retention percentage!

Let's take an example of a piece of PHP code:

$x = 5;
$sum = $x + $y;

This code will cause an error. Since the variable $y is not defined.

How Laravel Handles Exceptions

When using the Laravel framework, there are two layers to exceptions. The PHP exception handling layer, and the Laravel exception handling layer.

For the case of PHP exception handling layer, when the error occurs, the execution of the PHP program is halted, the error is logged to the "var/log/php/7.4/*" file and the error response is returned.

For the case of Laravel exception handling layer, when the error occurs, the execution of your code is stopped, then Laravel reverts to its global exception handler, which logs the errors to your selected channel, which uses file by default, and then response is returned, depending with the request type. If it is an API request an API response is returned, if not, the default, error page is returned.

PHP in general allow us to handle these errors using try-catch blocks, while in addition to that, you get the flexibility to handle different types of errors accordingly, with messages, logging and customized responses and response statuses.

How to use Laravel's exception handling.

How to Create Custom Exception Handling Classes

Following up with the same example above, in your terminal, run the command:

php artisan make:exception VariableNotDefined

This will create the file `app/Exceptions/VariableNotDefined.php with contents:


namespace App\Exceptions;

use Exception;

class VariableNotDefined extends Exception

For now you don't have to do anything with the file, but, let's head back to our code and modify it:

	$x = 5;
	$sum = $x + $y;
	echo $e->getMessage();

This code will by default just echo the error message. Currently now the error won't be logged to the PHP error logs, it only the error message will be echoed and the normal execution will continue. But we want to have a custom message if the error is due to lack of variable definition.

For that, we will have to modify the code a bit:

	$x = 5;
    if(isset($x) && isset($y){
		$sum = $x + $y;
    	throw new VariableNotDefined();
}catch(VariableNotDefined $e){
	echo "The variable was not defined, please define the variable first";
	echo $e->getMessage();

In this code, we have:

* Anticipated the possible error that the variable may not be defined
* Thrown an error that the variable was not defined
* Caught it in a custom exception handler and returned a custom message


The exception handler in Laravel is very powerful! You could also define custom views, pass data to exception handlers and more, which we will cover in the succeeding article.