Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:128330 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by lists.php.net (Postfix) with ESMTPS id AC2DA1A00BC for ; Thu, 31 Jul 2025 07:40:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1753947553; bh=b+puw7c0AVgAqW48QT32/QmRjhnUc+0TwsKg3A5v2Wg=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=j15BEOr/8bgvYTfxyanWdCoKlP3LDC/y4WlKpgbTFHlJNwj/7/g3kPd8th4JoF655 Puh1bhYKL5PiaQh5PQN/mmtS4cIjrdkSqE2rnLNeIVSJebuXY++AqXh4nSUG67bvl6 uLM1isbXHAIiTqeUcIAAleAICupP2K0Iiw4U5KIXyE4GcoTeLkBMAGa1q0sMSffDLr bAEufP7wBcKcYzEKRPoJQh64brpqTflMZigs2tV+IhWrQi/lUSYRIYHkY9oQ2MNjCu zsf/Q9i1MTsf7Rp+kqTn9W3DWLL3BGVswEAcxDSroaMieRu+R6/gWJO5ShSoyt09dx 7vL7STUX8f2nQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 7310C180339 for ; Thu, 31 Jul 2025 07:39:12 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.2 required=5.0 tests=BAYES_40,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) X-Envelope-From: <91liahim@gmail.com> Received: from mail-yb1-f169.google.com (mail-yb1-f169.google.com [209.85.219.169]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 31 Jul 2025 07:39:12 +0000 (UTC) Received: by mail-yb1-f169.google.com with SMTP id 3f1490d57ef6-e8fd59484f7so220934276.3 for ; Thu, 31 Jul 2025 00:40:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1753947653; x=1754552453; darn=lists.php.net; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=CdCSiRzo8292MJ9nFZHSQbCln0Zw2uulmfUU2p3v0oM=; b=gUcy6Ygf/2x3nVm6QkCDUWKi21RyWxIyGFmrCMiKnhVqmCsp1t+w5l2fNmdFtiyL1S xA970nZEevdcByZ4CEJ4hvvb6RYsu50Qow5pkJpIebE/bs1IL6PhXZ32Ftgv27lq3QqV nT1yqf5bT1T3RbV6B25qVvslWIL7xoqMPAq/jVSCPb08u9WEWbdrMjeeJ8DSRQubyNQu jDPrXRokEYOmmEGnqtmMEViZhs9aiWFkQua708nq4yxXAMyxdC6lNdlz1VJLKLiKQyd4 ZcToteas8kSGZDkpIimp/btY9OlArdO8ZIWBhav6QlN3vhWC4LkKz4wzc8Xpu1Z9i40o j9nA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753947653; x=1754552453; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=CdCSiRzo8292MJ9nFZHSQbCln0Zw2uulmfUU2p3v0oM=; b=n+KcvgS38tE55xeJ8jKspmitaID/Ff/Gu6WfSryyxuT2rkvrB+ZuUmGrAJOIDF7T7j VT2F3yZG5hMgeo++0MntZpa9fSY1UKtIxzktC3D9d0FrX8mVXrQ5VAzbmPIoQp+zoTlh L5M+orXGu9sHYs0bzmhxEUoahLdSwhOMjioBPYhxUutbBRTgFcJg36UXfKSF8Oa8Y7rH K8pcEaSGHieNE/4Rk7D0bJ19QXePp4b9gq/GvXVVcv2PTTOwOGT+5n3xYI/F3Ztf3VRb KdT3AGrjChjpn+OB3QhzCOciAjO7WsQ/91gmK6u9ReAnhLXmsdWH897nxXFGQnjmRUVN a2TA== X-Gm-Message-State: AOJu0YwzcvAirxKTVwZqh95POC0pQ7yzCujihPTAmSIi6+RwfC3Ni/RH tnj2NR3S6DM8O8/kBpa2qsl0Gb46/TDCj7V0n5DVRq77jaP0BIsNvJvdwSxt6lfT6RZdXYGEVTu En9lteRYp3M6doCCahUVxl3KYNFb4LBrHnoee X-Gm-Gg: ASbGnctSMvSI1qk6gZRQ839I/gWedeujZ8pLFIxzV+7xX1PRkzD6Iw6Oiun4y0TqGS0 n76sOhaggucFomWtuN7v48HqYKxFn/14oYbW8hCenYaAtMDl7NOzaUP4dsayHcW9aAs7/V+sQLz yJd/TlLDvp5gGaplN5R+CzAixqF0JmBa1YyPkAUIxrrEbEVYg9YwKwwwOd/l1m5gUi8BEBndL4U kMvwpE= X-Google-Smtp-Source: AGHT+IGwdrPPo7vgvzqf8hsh3U0cNsLZzA7DSLERZ5e/to5acl8Wmwv8MUzfMrViiDxYodAcU7bLzBvFsEvax+3MjbI= X-Received: by 2002:a05:6902:1107:b0:e8f:d3ff:fc7f with SMTP id 3f1490d57ef6-e8fd3ffff0cmr1560519276.10.1753947653479; Thu, 31 Jul 2025 00:40:53 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 References: In-Reply-To: Date: Thu, 31 Jul 2025 12:40:42 +0500 X-Gm-Features: Ac12FXy8IFjiSMbbcm0zFHQZj8MvwNod2jKq1Un1JMm6MUfGqF2o6WYtj0GfU5g Message-ID: Subject: Re: [PHP-DEV] [RFC] Optional Catch Block Body To: Rob Landers Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary="000000000000d72134063b34c224" From: 91liahim@gmail.com (Mihail Liahimov) --000000000000d72134063b34c224 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi, Rob! Good point! I also know one another case when empty catch block are useful. When we want to avoid returning null values, we resort to using exceptions so that the client code can somehow handle the absence of values. For example: if ($user->getName() !=3D=3D null) { $this->doSomethingWithUserName($user->getName()); } We usually add an exception inside such getters if there is no value: try { $this->doSomethingWithUserName($user->getName()); } catch (UserNameIsNull); This is useful when we need to "do nothing" when there is no value. In my opinion, this code looks more declarative. We can do something like this also with repositories: try { $post =3D $this->postRepository->get($postId); // do something with post } catch (PostNotFound); But again, there is a fine line between situations where an empty body of a catch block is reasonable and when it is not. =D1=87=D1=82, 31 =D0=B8=D1=8E=D0=BB. 2025=E2=80=AF=D0=B3. =D0=B2 12:13, Rob= Landers : > even in my case, my empty catches have lengthy comments describing why > they are empty in case a user steps into it, so they can better understan= d > how the framework works. > > =E2=80=94 Rob > --000000000000d72134063b34c224 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi, Rob!

Good point! I also k= now one another case when empty catch block are useful.

When we want= to avoid returning null values, we resort to using exceptions so that the = client code can somehow handle the absence of values.
For example:
if ($user->getName() !=3D=3D null) {
$this->doSomethingWithUserN= ame($user->getName());
}

We usually add an exception in= side such getters if there is no value:

try {
$this->do= SomethingWithUserName($user->getName());
} catch (UserName= IsNull);

This is useful when we need to "do nothing" when = there is no value. In my opinion, this code looks more declarative.

= We can do something like this also with repositories:

try {
$post= =3D $this->postRepository->get($postId);

//= do something with post
} catch (PostNotFound);

But again,= there is a fine line between situations where an empty body of a catch blo= ck is reasonable and when it is not.

=D1=87=D1=82,= 31 =D0=B8=D1=8E=D0=BB. 2025=E2=80=AF=D0=B3. =D0=B2 12:13, Rob Landers <= rob@bottled.codes>:
=C2=A0even in my case, my empty catches have length= y comments describing why they are empty in case a user steps into it, so t= hey can better understand how the framework works.

=
=E2=80=94 Rob
--000000000000d72134063b34c224--