Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch exception in controller in Laravel?

In my controller i need to test custom database connection and if it's bad return an error. The problem is that catch block doesn't work... I get an exception message which is globally defined in app/start/global.php.

try {
    DB::connection('test');
} catch (Exception $e) {
    dd('error');
}
like image 836
Kin Avatar asked Oct 19 '22 06:10

Kin


1 Answers

Laravel handles exceptions, no need for try/catch. You can write a custom exception inside \App\Exceptions and customise the contents of App\Exceptions\Handler.php to format the response of any individual exception to meet your needs.

like image 54
prime Avatar answered Oct 21 '22 23:10

prime