4

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.

1
  • After adding below code: App::error(function(InvalidRouteException $exception) { \Log::info('InvalidRouteException'); $code =404; $data = array('codeError' => $code, 'messageError' => 'Whoops, looks like something went wrong.'); return \View::make('404')->nest('content', 'error', $data); }); I get error route home not found exception, i am not able to see the view. I need to create a custom page where i can see the custom exception. Commented Apr 24, 2015 at 7:10

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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 {}

1 Comment

I like this approach better, if you want to throw a lot of exceptions in your application it's better to put them in a separate file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.