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

phpMick's avatar
Level 15

Should I wrap my Http requests in a try/catch?

Hi,

I'm currently having an internal debate about this.

If I have this:

try{
	$response = Http::get('http://example.com');
} catch (Exception $e){
	//Log the error
}

Do I need the exception handling? I can just check what is returned ( $response->successful()).

I usually would but am wondering if I need to. The docs say: If the given timeout is exceeded, an instance of Illuminate\Http\Client\ConnectionException will be thrown.

I guess it also depends on what action I want to take on failure.

Cheers,

Mick

0 likes
3 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

I guess it also depends on what action I want to take on failure.

Yes. But most of the time you just want to let laravels exception handler handle the error. If you want something specific to happen on ConnectionException exceptions, then update the exception handler

https://laravel.com/docs/9.x/errors#rendering-exceptions

phpMick's avatar
Level 15

Yeah, so If all I am doing is logging, then it's pointless (the Exception will get in the log anyway).

Sinnbeck's avatar

@phpMick Yeah that should be handled in the exception handler, not in the code that call the http client

Please or to participate in this conversation.