Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

thebigk's avatar
Level 13

Help me understand withExceptionHandling() and withoutExceptionHandling()

Can someone briefly explain when should we add $this->withExceptionHandling() and $this->withoutExceptionHandling() in our tests? Laravel has added this functionality out of the box in 5.5 and I'm having a hard time figuring it out.

0 likes
2 replies
riatec.kp's avatar

I think this is explained in 'The Exception handling conundrum' lesson in 'Let's Build A Forum with Laravel and TDD'

Thanks

1 like
martinbean's avatar
Level 80

@thebigk The methods do exactly what they say on the tin: they run your tests with exception handling, or without.

Without exception handling is handy when doing HTTP tests, like:

$response = $this->get('/foo');

$response->assertSuccessful();

Your test is asserting the response status is successful, but you might get a 500. But by default, if you do get a 500 status you don’t know why. So if you disable exception handling, it will instead log that exception to the console for you to see why and then fix it:

$this->withoutExceptionHandling();

// If this throws a 500 error, it’ll now display in the console instead
$response = $this->get('/foo');

// This won’t be called, as the exception above will halt execution
$response->assertSuccessful();
21 likes

Please or to participate in this conversation.