Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:109206 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 21927 invoked from network); 22 Mar 2020 19:51:05 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 22 Mar 2020 19:51:05 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id AEF081801FD for ; Sun, 22 Mar 2020 11:15:01 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-qt1-f173.google.com (mail-qt1-f173.google.com [209.85.160.173]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sun, 22 Mar 2020 11:15:00 -0700 (PDT) Received: by mail-qt1-f173.google.com with SMTP id m33so9768917qtb.3 for ; Sun, 22 Mar 2020 11:15:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=newclarity-net.20150623.gappssmtp.com; s=20150623; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=6nBBbO4YQHC3tAXCG2t2KcLFXYq1VGVyxcJcQxWdvb8=; b=Y0cgzzRdSFIUhtRMsS9gokAB4o87UHziP8lpdYZQTf98g+fO/IzuJ4BdCZKaNBDdBx Kn3+c/HNuxEPeVBE99dCl7DLmVBMKHXQ4tleQgtck8KmeZBsd+NeI0l/ca4wwmKTONym mnhHjArnSO+dQ2NYFP9mXbkeeX1M0wSdPLqQwM2Jaspn5E2K0tsQijaYoEAKFB4GO6Ki eErefehpbC9lbT7+TZ2q6ZYQkJMAKu3qQFmCXIQYzNtnwmNlivTH3E+aWBOOkqXmP6Ho TabWibXRJJR19sj62Yub2ndu+EiKGwNT3sceRbhaqQjqkIYSpuroCiNT6AtrD/6cjL8B CgUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=6nBBbO4YQHC3tAXCG2t2KcLFXYq1VGVyxcJcQxWdvb8=; b=rfAwlE1vamLWxNSv0yeHFBGeIEC5outLKa523B6I+eUhJkI45MjiCaTA4kZZf8IDDC NbhaCD2ObrhlFHq7OuCq0atfbPVp6s5mTJgTg9jPKWvJsjnqxN0K26lUlL9duRjC65Wr y/ErtgQhFsp5BQE4/LvBaC2thVcCuQP/S0rkeCv3YfV6vROrcqB5W1GGhQPT8CNXqO9m xJuC43m6G99YdaBYV/T9RJ2rGfGNgPHqpWHFgihIgaEdw6Lvem8WEmdZki1snYlHG+A9 ywJbBDF8P8fzzBB8Mte84QCbkUAjCnhgBR3sCq5aH2XBd+GGLb48THT+JSA44/QEUDvC HGmA== X-Gm-Message-State: ANhLgQ1P/hv3OPUxrX/9rYaJCd+XVS3xMg1KNO8T/br1+TW2qb9Eoq1m bulBKQC+6miTmT13t4Fo87j/wg== X-Google-Smtp-Source: ADFU+vsJF8YfEd6CE0Acl3FUwtN62jabyNfTPFeCPaGbJGRf/Oaez5X/LUjRQEFwQWlmCqI55ClHiQ== X-Received: by 2002:ac8:1b6d:: with SMTP id p42mr8581785qtk.345.1584900897853; Sun, 22 Mar 2020 11:14:57 -0700 (PDT) Received: from ?IPv6:2601:c0:c680:5cc0:b161:bce7:21ab:aa25? ([2601:c0:c680:5cc0:b161:bce7:21ab:aa25]) by smtp.gmail.com with ESMTPSA id j50sm10506736qta.42.2020.03.22.11.14.56 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 22 Mar 2020 11:14:56 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.11\)) In-Reply-To: Date: Sun, 22 Mar 2020 14:14:51 -0400 Cc: Ilija Tovilo , PHP internals Content-Transfer-Encoding: quoted-printable Message-ID: References: <5BEF8264-4003-4056-A31C-FF8BEE85FED6@me.com> To: Dan Ackroyd X-Mailer: Apple Mail (2.3445.104.11) Subject: Re: [PHP-DEV] [RFC][DISCUSSION] throw expression From: mike@newclarity.net (Mike Schinkel) > On Mar 22, 2020, at 1:16 PM, Dan Ackroyd = wrote: >=20 > On Sun, 22 Mar 2020 at 16:17, Ilija Tovilo = wrote: >>=20 >> Due to the modest feedback I=E2=80=99d like to move the throw = expression RFC to =E2=80=9Cunder discussion=E2=80=9D. >>=20 >> https://wiki.php.net/rfc/throw_expression >>=20 >=20 > Regarding the example: >=20 > $condition || throw new Exception('$condition must be truthy') > && $condition2 || throw new Exception('$condition2 must be truthy'); >=20 > The "Deprecate left-associative ternary operator"* RFC made it so that > parentheses are required when ternary operators are nested in > complicated statements. >=20 > Would a similar requirement for parentheses around complicated throw > expressions be a suitable solution to avoid people being surprised by > the behaviour? >=20 Why can't you just do this in userland code? function throwException(Exception $exception) { throw $exception; } $callable =3D fn() =3D> throwException( new Exception() ); // $value is non-nullable. $value =3D $nullableValue ?? throwException( new = InvalidArgumentException() );=20 // $value is truthy. $value =3D $falsableValue ?: throwException( new = InvalidArgumentException() ); // $value is only set if the array is not empty. $value =3D !empty($array) ? reset($array) : throwException( new InvalidArgumentException() ); -Mike P.S. I am probably in the vast minority on this list but I would like to = see fewer places that throw exceptions, not more.=20 I want to deal with errors where they happen, not throw exceptions or = have to catch them as I have found that I can write much more robust and = easier to read code when I write without using exceptions. I came to = this realization because of learning that Go does not endorse exceptions = and then I learned why they do not which strongly resonated with me. = After that, I finally felt comfortable saying that "Exceptions seemed = like a good idea at the time." I now have a whole slew of classes who only purpose is to wrap PHP = functions and classes that throw exceptions so I can call them w/o = having to use try{}catch{}. Instead I use an if() afterwards to check = and then handle it if there was an error. One particularizing problematic exception in PHP is with the DataTime = class. I have found it effectively impossible to create a class that = extends DateTime without having the potential for an exception to be = thrown (unless someone knows a way that I do not?) The potential is = actually hypothetical, but PhpStorm nonetheless still complains that I = have not handled exceptions when using that child class.=