uday8486
uday8486

Reputation: 1393

laravel custom exception handling

I am new to Laravel framework. I need to add a custom exception error handler. I tried to add below code to my global.php file:

App::error(function(CustomException $exception, $code)
{
echo 'Debug: CustomException<br/>';
});

To throw this error I used below code in one of my controllers:

throw new CustomException();

But I am getting error as CustomException not found.

I googled it for solution, but everywhere I find the same solution.

Please help me to get this fixed.

Upvotes: 4

Views: 5663

Answers (2)

E. S.
E. S.

Reputation: 2919

You can create your class (Ex.: app/Exceptions/MyCustomException.php), then add to composer.json autoload files.

"autoload": {
  "files":["app/Exceptions/MyCustomException.php]
}

Then: run a composer dumpautoload

Now, you can use your MyCustomException class.

--

<?php 

class MyCustomException extends \Exception {}

Upvotes: 2

laurent
laurent

Reputation: 90736

You need to define your custom exception first. So at the top of your global.php file, add this:

class CustomException extends Exception {}

Of course, you can add custom properties and methods to this class as needed.

Upvotes: 2

Related Questions