Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:128331 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 A43BC1A00BC for ; Thu, 31 Jul 2025 07:59:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1753948650; bh=Yomt9c/qH/sDc44XUIjGXvI0Yy1WgsPzHSCkWhPDFjs=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=D1Ybwf4mZW40/yhpkR9CCKTd8ZJewsfZwDVomFnWUlOL/Y7V7tp8gCKvXcUWjOBC2 UcG8kezjKrxFUMFgN1RhNsx6XwV7bJ0kZ2gaGQllWqPNRwAyGKhr9DYZwfpU/y15Mp Wb3HX3xH6gezhMBaowWrYBJbPBhkFDW/stz+FU49jPMAe196wTcaI5ESgvCijQMbBt oRebVSzhHGFfqwjdGp5gf6RSPTtC3nBPcx+Aq+a8YS1eiPuRH6DfQyYwpVcKgfd1nX 65k3Yn4T/+19lFM13uAsErrDjHfluV5QROm1sli1VxNTezSNMeeAdFUdZKVtL+Vseu GNHdm4MOejIDA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 93CC71801D8 for ; Thu, 31 Jul 2025 07:57:29 +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_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: Received: from mail-lf1-f49.google.com (mail-lf1-f49.google.com [209.85.167.49]) (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:57:26 +0000 (UTC) Received: by mail-lf1-f49.google.com with SMTP id 2adb3069b0e04-55b8248e77bso86313e87.2 for ; Thu, 31 Jul 2025 00:59:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1753948747; x=1754553547; 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=wCwfr1X7WyarbXVdOCjxbzaz6GoKYP2Sq3/LT4J4Rkc=; b=nTn7OXDmzQUPeSXoK8opjEDOZedatUd1/0s/tMEsA4RgaDnfh3kEhhhIiEerWJQ8IP 6yKYKbB9u1qj+lxlswaXHV8ArTCWvG8kONC9L09OjBFbdcKYdxwf4Sca5MRUNxZVNHHz 2sakpU3bWt5pIsG/IgmZZrIcLLi74ncBg5Wt5wL8aOVMJF5LtX3c6gftwBumX5TCJOht 9avNijYbhlbZdrHenmLmSHslZxdoMGTEgvzUgQ62IdrsFKqMQlbSoFGIU9HllFZewroi RV4Azgd+UpTwLkUwwGLUFVtWbyR+pMLqlZGCcGMiu/F420kVAgwJvG43snzORNJcmbMw sc8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753948747; x=1754553547; 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=wCwfr1X7WyarbXVdOCjxbzaz6GoKYP2Sq3/LT4J4Rkc=; b=Ms4dXYGPBaXiCBky3KOgnIN0O+AWNaJ5+tjD2p1K9q4nciynI2XlGtaOOjuibnMWBg HDcEIuRCUy46bHOHCJ4EN+9YnmpHuVWCViF8qGWTytxYZeJYYsgZy2NhztWUr8D1G+uL l5KCxnssQpB+Sczx/nbGEXtRYTJnndAx9IdgOjnF8fKpLnF/3oMyQrVYyH8sU5J9pIO1 wKM/wsR/yqQtn7e3bb9Kfpjch2IgMki8f7WvKYH3idE8RXcGxr4zJKZJSJzRsUdtJAaz iKPnPVFhW9NN4RiEjnR9DJGy/etdWClgeQ9b/lapAn9h5bYnb1bIXg/MM0qXJixLuTs1 FTPg== X-Forwarded-Encrypted: i=1; AJvYcCUPQrIQGeX936q+zsFrg+WCYSJuxA3YUkukd7xoTKSOlubiS4mtk12LRtv6sc4KbqWpbgvZvPYIhgE=@lists.php.net X-Gm-Message-State: AOJu0YywI/iSH7MjfOso1EsAzY6Srk8JkjKjeI28digReyTeM4ILrOjB c7ZhX78d3nY4WJ20BB2qNK/VEzpVa/ivQFoue1dtzbGj2NHFA1lFPgDxPFpRjVo95WeJJHGrml4 SM1XZE3wcP5QBLOKM/TogifucAGTrbv0= X-Gm-Gg: ASbGnct6zv3rv65P5TM3OfXebxEmFrERpRUobaItBnygFhaJQlXMDX3wgLVUuDOiKPV pOQy5uRTDef5lWnH88RvsGmSt/Z3oMewWwG8KaVczBN2WF6x1Mk9MR0VQ+qIFE3wtR7cnNL9Ykj 9U9EqVOFPhs7UpBcM1Fv6QcRgeO8jper9ZbYpy8pv3jZwUEPR+8IxbNpsRgdglmGqTBDBp6RJNu bOM2y9geX3CHZZKeVk= X-Google-Smtp-Source: AGHT+IEeni/Tjst96Qwpk3w+h9YpsOEyL6Zc35cTU59XHpCO8gNlyk8rQH7boE9M8dkEQpx5J23F7fzqDRP5ltb3950= X-Received: by 2002:a05:6512:b0d:b0:55b:8205:b21f with SMTP id 2adb3069b0e04-55b8205b46cmr1363616e87.19.1753948746324; Thu, 31 Jul 2025 00:59:06 -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 08:57:00 +0100 X-Gm-Features: Ac12FXyGLopiy0RKOzzymmi6DAztVAiylhHJsgn8-jrng3b5n9Hs4rQX064a1vo Message-ID: Subject: Re: [PHP-DEV] [RFC] Optional Catch Block Body To: Mihail Liahimov <91liahim@gmail.com> Cc: Rob Landers , php internals Content-Type: multipart/alternative; boundary="000000000000faa08a063b3503c1" From: oladoyinbov@gmail.com (Oladoyinbo Vincent) --000000000000faa08a063b3503c1 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Interesting catch, but i have been thinking of a better approach of catching errors without the try and catch block madness. Here is my suggestion: ```php try (Exception $e) { // define logic } if ($e) { // execute code... } ``` looks way more clean and structured, what do y'all think? Just a suggestion though. On Thu, 31 Jul 2025, 8:41=E2=80=AFam Mihail Liahimov, <91liahim@gmail.com> = wrote: > Hi, Rob! > > Good point! I also know one another case when empty catch block are usefu= l. > > 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); > > 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, R= ob 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 understa= nd >> how the framework works. >> >> =E2=80=94 Rob >> > --000000000000faa08a063b3503c1 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Interesting catch, but i have been thinking of a better a= pproach of catching errors without the try and catch block madness.

Here is my suggestion:

```php
try (Ex= ception $e) {
=C2=A0 // define logic
}

if ($e) {
=C2=A0 // execute code...
}
```

= looks way more clean and structured, what do y'all think?

Just a suggestion though.
=
On Thu= , 31 Jul 2025, 8:41=E2=80=AFam Mihail Liahimov, <91liahim@gmail.com> wrote:
Hi, Rob!

Good p= oint! I also know one another case when empty catch block are useful.
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->doSome= thingWithUserName($user->getName());
}

We usually add a= n exception inside such getters if there is no value:

try {
$this->doSomethingWithUserName($user->getName());
} c= atch (UserNameIsNull);

This is useful when we need to "do nothi= ng" when there is no value. In my opinion, this code looks more declar= ative.

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 <rob@bot= tled.codes>:
=
=C2=A0even in my case, my empty catches have lengthy comme= nts describing why they are empty in case a user steps into it, so they can= better understand how the framework works.

=E2=80=94 Rob<= /div>
--000000000000faa08a063b3503c1--