Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107655 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 65165 invoked from network); 24 Oct 2019 14:46:32 -0000 Received: from unknown (HELO php-smtp3.php.net) (208.43.231.12) by pb1.pair.com with SMTP; 24 Oct 2019 14:46:32 -0000 Received: from php-smtp3.php.net (localhost [127.0.0.1]) by php-smtp3.php.net (Postfix) with ESMTP id 209252D20B9 for ; Thu, 24 Oct 2019 05:32:56 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp3.php.net X-Spam-Level: X-Spam-Status: No, score=-1.1 required=5.0 tests=BAYES_00,BODY_8BITS, RCVD_IN_DNSWL_LOW,SPF_HELO_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS3265 194.109.0.0/16 X-Spam-Virus: No Received: from lb3-smtp-cloud9.xs4all.net (lb3-smtp-cloud9.xs4all.net [194.109.24.30]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp3.php.net (Postfix) with ESMTPS for ; Thu, 24 Oct 2019 05:32:55 -0700 (PDT) Received: from [IPv6:2001:983:6fc5:1:3cbd:5793:795b:352d] ([IPv6:2001:983:6fc5:1:3cbd:5793:795b:352d]) by smtp-cloud9.xs4all.net with ESMTPA id NcIOitztGsBskNcIQirPCa; Thu, 24 Oct 2019 14:32:54 +0200 To: Mark Randall , internals@lists.php.net References: <201910211719.x9LHJluS018629@mxdrop307.xs4all.net> Message-ID: Date: Thu, 24 Oct 2019 14:32:52 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0 MIME-Version: 1.0 In-Reply-To: <201910211719.x9LHJluS018629@mxdrop307.xs4all.net> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit X-CMAE-Envelope: MS4wfJ7i0/lIQ0aFqNRcIuvm0ZWUkwKGVyk6Tv8I26hxkf0jZXo75braqLMi6mx+w84WfW+KShSoS6OYsPwVQjli5RcCl+Bszw3RxIDH3uRl/EZfhEsbRLs1 lUVcZ6bTeyjSbTwKNBzbctAH54j2UKW7sk3EWCDZ2Hcf8aLgIIn703X6J6LeXVKnPYj/jsR8vr6+a8LatYho45FscqwxCx35Ry+CbO6AWwoN/u7VeQ3BqKNr McK40isd5ahcc9oriL/cWLmtzPT6TE8l4fKEXL1L8YWxs653+jphaB2eT0SVS/KM X-Envelope-From: Subject: Re: [PHP-DEV] Reclassifying some PHP functions warning as exceptions From: d.takken@xs4all.nl (Dik Takken) On 21-10-19 19:19, Mark Randall wrote: > On 21/10/2019 17:02, Rowan Tommins wrote: >> - They immediately jump control out of the current frame of execution. >> Unless you put a separate "try-catch" around every line, there is no >> "acknowledge and run next line". > > I've been toying with the idea of: > > $x = tryval fopen('missing.txt', 'r'), >             FileException => null; > > if ($x === null) { >   die('File could not be opened.'); > } > Or maybe introduce a try() function that can wrap calls. Since most - if not all - functions would throw a single type of exception, the following would catch that exception and return null in stead: $x = try(fopen('missing.txt', 'r')) An optional argument could be used to return a different error value: $x = try(fopen('missing.txt', 'r'), false) I guess that would cover most use cases and make it easier for people to accept an RFC on this subject. For cases where a function can throw multiple exceptions, the syntax could even be extended to allow this: $x = try(fopen('missing.txt', 'r'), [FileException => false]) Here, try() would swallow only FileException, other exceptions are still thrown. I'm not sure if this construct is worth introducing though, the difference compared to a proper try / catch is much smaller. A try() function could also help provide a migration path to PHP 8. It may be possible to write an automatic code fixer that wraps any function calls that need it in a try() to retain old behavior. Backporting try() to PHP 5.x would allow the same code to run on both PHP 5 and 8. Regards, Dik Takken