Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:128338 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 AF0971A00BC for ; Thu, 31 Jul 2025 09:21:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1753953595; bh=FpFXQW3pOTRe54QGxzrHrQqe1sw5L/6mllYzPa5kfvI=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=bYh6lWtd5JLPtd/C1jHNWvT4ftY3i+Jzt/6yZz6qHUfjIcD+eaFwwTxz2l0Xn6gA3 prvW3Fea4dft+LtikX0MJT4Bd8oJvdA/feSemrc9l8g/gQsnN+tYTEiGM4tgUx7Ic+ c8glvy9I700kCTKOhw08j6AY1RrcqHI0sJX53nLXF8H9htYby1Pihfe4ShI6HevYwD RFiQ9cRcJXnhkPVB04D5CXB63M4etogD4ZI5O5+JM4eHZrv68pkv2KtjFWI7aXclqa GTrESRYukSsPk9C50R3yHhu0I86ywxoRfwo/gMMMUEOWOluZkeRLvopMm1LzR4qJR1 zE6szEzABiHCw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id CDA6D1801DA for ; Thu, 31 Jul 2025 09:19:54 +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=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, 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: Received: from mail-pg1-f177.google.com (mail-pg1-f177.google.com [209.85.215.177]) (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 09:19:54 +0000 (UTC) Received: by mail-pg1-f177.google.com with SMTP id 41be03b00d2f7-b1fd59851baso121490a12.0 for ; Thu, 31 Jul 2025 02:21:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1753953695; x=1754558495; 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=FpFXQW3pOTRe54QGxzrHrQqe1sw5L/6mllYzPa5kfvI=; b=d6oQeGkiJ6i91obUOf7CQZiXynwmAiSpavzQzLE0W0k67LSnCbTw426yIoaufelAaU ySkkz5pXaD/CIGKs8wzg/iPMm2fnm3nj2w/rB6qapoJlcZzOIf5P2OO2W/Em8pw1y2wB dNPZlvoalCb4RjVIaWOkgPD5MTSDmZqY42hmHhbJZ9dYRSoIjTAyFgguy7aHHBnX7Y4q Mml8JAu4w8LfRNQDHL97Iy3LWPV3UsbF3KByNCgaS93W0DF2MArIRO9uhoPY5N3WT/bt 7W5TJweIwPyEjMcqT59JJ0lRLhPHfwB9S6iPpsD6+w5bRUb/30F341e5QlT5oYXpQq70 ZjUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753953695; x=1754558495; 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=FpFXQW3pOTRe54QGxzrHrQqe1sw5L/6mllYzPa5kfvI=; b=Kwn7clJOcGfW09tQFENTgsVWuozXmRAOBUdIleT9BpWdb8rN5ptG11+d3LXmdquEzq QclOM+Qkio0q84Wmj28g47c9rDaPkA4TqWsbt2thDxW5fVI0NPxa3PoYMdK/BjJry3MM iTpCGVq73+I9rGPTV52Tl+0XqpoGYDpLBG6bYX2oO2i5ifXas5nPKmd67HhnAA7he8fn ZWaGBnumu+0B7mjL6r0jpxo6CQB58xi0BWIJuv1XMqCi2orQGU+SwYU7aMFzvlKc0xTO 1k8j2sYZC4uVohn/wfJPEjJEpHFG56D2Qxf696nHwHOVSfjcqfXlSD4Is25m7xZutVRt EsiA== X-Forwarded-Encrypted: i=1; AJvYcCW5V0LzQqeetuUyzAsUYnM/JUOtARRgDGEibUrYDKW83GmSRMMSXgBtjxBoUhnB0Pd+L/V3gLz2R6I=@lists.php.net X-Gm-Message-State: AOJu0Ywy4/YYPctJTAo99qpEZyRpshHa/SJ+Tst48J5IBvyObYUE/COy VVIht8FyrwdXHSxnRNWICmwr1TVlARyr6addkTEEnvvpRAxsAajp+EtU/lf1y2fERUK/cANRfuS hgu/mfFdoCQgfkFMvOWrABKuVhFGSe/4= X-Gm-Gg: ASbGncv/1vbkXsJClP+4WvG2OD5EbEV/XTlnPk4onlbdXfmvTv4UBhAAnZ97UB2r10s tZahO8jfMK9XYsJyqmSXT9CBZ59ic8N1n8csD1Z9pEh8TmllckXFOZwkmnHlRuH/XQM4Mri0deH f6uSeV/NNJ5Erqexg0uPbO4ZE+fmzNKdXbRelItjqJoQhizdnmf9RaIgBgK8Dm+danoGQqtwHYB XPnNadf X-Google-Smtp-Source: AGHT+IF9OGX3oIB3mDg4LxlOyI5VEhYS3JeqDyDa6sMjb5LNzhnz/E/NX9zk13MLo+Z+/dL1Gr3nh/Y1U7VgKYf5K6w= X-Received: by 2002:a17:90b:3a08:b0:31e:8203:4b9d with SMTP id 98e67ed59e1d1-31f5ea4cc0cmr8783124a91.29.1753953695232; Thu, 31 Jul 2025 02:21:35 -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:21:24 +0300 X-Gm-Features: Ac12FXwf4CVSlHCYm6xl37KyVMhp2hYjrTTJYW5Z__D02q9DRDipU_AtmUxXBCE Message-ID: Subject: Re: [PHP-DEV] [RFC] Optional Catch Block Body To: Mihail Liahimov <91liahim@gmail.com> Cc: Rob Landers , internals@lists.php.net Content-Type: multipart/alternative; boundary="000000000000f4f95f063b362a6a" From: xepozzd@gmail.com (Dmitry Derepko) --000000000000f4f95f063b362a6a Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, Jul 31, 2025 at 10:42=E2=80=AFAM Mihail Liahimov <91liahim@gmail.co= m> wrote: > Hi, Rob! > > When we want to avoid returning null values, we resort to using exception= s > 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); > There will be TypeError because "doSomethingWithUserName" should accept strings. Using exceptions to check types is rough practice because exceptions aren't cheap. "if" is cheaper. > > 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); > Instead of idents inside another block I'd prefer to use early returns: try { -- ... -- ... -- ... -- ... -- ... } catch (...) vs try { -- ... } catch (...) { -- return } ... ... ... ... Or even "if" if (!...) { -- return ... } ... ... ... ... --=20 Best regards, Dmitrii Derepko. @xepozz --000000000000f4f95f063b362a6a Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Thu, Jul 31, 2025 at 10:42=E2=80=AFAM = Mihail Liahimov <91liahim@gmail.co= m> wrote:
Hi, Rob!

When we want to avoid returning null values, we r= esort to using exceptions so that the client code can somehow handle the ab= sence of values.
For example:

if ($user->getName() !=3D=3D nul= l) {
$this->doSomethingWithUserName($user->getName());
}=

We usually add an exception inside such getters if there is no valu= e:

try {
$this->doSomethingWithUserName($user->getNa= me());
} catch (UserNameIsNull);

There will be TypeError because "doSomething= WithUserName" should accept strings.=C2=A0
Using exceptions = to check types is rough practice because exceptions aren't cheap. "= ;if" is cheaper.
=C2=A0

This is usefu= l when we need to "do nothing" when there is no value. In my opin= ion, 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);

Instead of idents=C2= =A0inside another block I'd prefer to use early returns:

=
try {
-- ...
-- ...
-- ...
-- ...
-- ...
} catch (...)

vs

try {
-- ...
} catch (...)= {
-- return
}
...
...
..= .
...

Or even "if"
<= div>

if (!...) {
-- return ...
}
...
...
...=
...



--
Best r= egards,
Dmitrii Derepko.
<= /div>
--000000000000f4f95f063b362a6a--