Kin
Kin

Reputation: 4596

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');
}

Upvotes: 5

Views: 7357

Answers (1)

prime
prime

Reputation: 2074

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.

Upvotes: 3

Related Questions