Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107616 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 40439 invoked from network); 21 Oct 2019 22:52:34 -0000 Received: from unknown (HELO php-smtp3.php.net) (208.43.231.12) by pb1.pair.com with SMTP; 21 Oct 2019 22:52:34 -0000 Received: from php-smtp3.php.net (localhost [127.0.0.1]) by php-smtp3.php.net (Postfix) with ESMTP id 99AEB2D2015 for ; Mon, 21 Oct 2019 13:38:18 -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=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,HTML_MESSAGE,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS3215 2.6.0.0/16 X-Spam-Virus: No Received: from mail-io1-xd32.google.com (mail-io1-xd32.google.com [IPv6:2607:f8b0:4864:20::d32]) (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 ; Mon, 21 Oct 2019 13:38:18 -0700 (PDT) Received: by mail-io1-xd32.google.com with SMTP id z19so17682810ior.0 for ; Mon, 21 Oct 2019 13:38:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=iqslWPso1RN64tl9BQ1DZEVj24Dib0+1g1kWRpj/n/0=; b=darjgvoY3yFMjUG5ArPpyvbJB3DtodGTlY2xOwGmcT7PwFCpzXXpURYrToSEbXCQfB Bl3C8rR/FqCsK/qrVgC854BeDYy0dblChJLtPanJGFrbN163qRG4mLKf3Vhct2W3RaQi 312QpwMVxTCIThP6MiZYhhuSaFO5lwbHtYBrd7mgI8RQraifN/nsNUJPxZcommUDgqli 8TP/heAo9IXeHIzWbAARmHXwCjsl4v4AhTa05Lwh8Pp2zTJ/nXdAwRw4OEbkbPS+lEdz BfwYlBLqckatp4GmsaOMALyK/566xv9nT2wFxmNntPZkJEpd8ESqP4HLr2oXs45jdJFr 5u3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=iqslWPso1RN64tl9BQ1DZEVj24Dib0+1g1kWRpj/n/0=; b=HwwA9neC73181rSgC2mVNfqROkHcx9h+NnHmEaSIOOYQ7wW2r3E7EXRVLoo9QPXjZ1 gqaDuxtpwoqbHanu3IS9fJ2QmJ2V46CIWeQ7j14diOsjFZi2s1W5G3cyR0EAgo5G+GIl wFZwhpUVOs8ZLdxhs9Cu7S2/VXhRqiZKXS1yqmoltyqBSXESB/NXircviV0+7m5a55g4 gc5Csv4bpcWdlEvreKSVKlp4bZI3ECyesGg1tbjk71EfGZmGjMPMJbNDybXelW9uA4i2 592O164j2fEwH9p0rhhYQFTpMLv9ZhdTyysLHgRuhbHJXU6ECdFgfxie+GqEz/v/7yZO /esw== X-Gm-Message-State: APjAAAWWPgUlbBvshC3wBX+1oFVYTHhdz0CqurhI/0+VHt7mDNAkCOgO 82rvwXBkBIh2Ro3/BNJFET5coLjJk7y5J1Z6ycQ= X-Google-Smtp-Source: APXvYqxVTh4pdR/7RnePBEyhdr4/9sDLJV5yqZL5Isqpi8XXsggbIZICxjLiV30V2Kni9VVmx2k78T82YGXIVTPfs40= X-Received: by 2002:a05:6638:6b8:: with SMTP id d24mr177868jad.135.1571690297514; Mon, 21 Oct 2019 13:38:17 -0700 (PDT) MIME-Version: 1.0 References: <6fa02cb5-b474-4b36-4e0f-89679475e250@gmail.com> In-Reply-To: <6fa02cb5-b474-4b36-4e0f-89679475e250@gmail.com> Date: Mon, 21 Oct 2019 22:38:01 +0200 Message-ID: To: Rowan Tommins Cc: PHP internals Content-Type: multipart/alternative; boundary="000000000000e15cd4059571a929" X-Envelope-From: Subject: Re: [PHP-DEV] Reclassifying some PHP functions warning as exceptions From: benjamin.morel@gmail.com (Benjamin Morel) --000000000000e15cd4059571a929 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable > > I think this argument is something of a logical fallacy: just because > exceptions are (or can be) more fine-grained than the current return > value, that doesn't mean they're the best tool for the job. > If I'm remembering it correctly, an example is the old PEAR > error-handling model, where rather than false, functions returned a > PEAR_Error object. This could be just as fine-grained (using sub-classes > or error code constants) as an exception, but without the additional > behaviour of blowing through the stack. Sure, you can do without exceptions. I think what you're suggesting is similar to Go's error handling. But PHP at some point decided in favour of exceptions, so it would be logical to pursue in that direction. On the other hand, there are some errors where the answer to the > question "would it be better to halt processing than continue in an > unknown state?" is "yes", so it's reasonable to introduce an exception, > even though existing programs aren't expecting it. But again, this > should be considered for every case, not as a general rule that all > Warnings should be promoted. I would classify most, if not all, filesystem-related functions as mostly "yes, do stop execution by default when something fails". So this is, as well, in favour of exceptions. To take the mkdir() example, the only reasonable failure reason I can think of that would not justify throwing an exception, is when the directory already exists. So I would be OK with this: mkdir(): bool; // true if successful, false if the target directory already exists This is, IMO, the only time we can safely proceed while ignoring the outcome of the operation, as the filesystem is in the same state whether or not the function succeeded (the directory exists). All other outcomes are, IMO, exceptional situations, and throwing an exception is a safe way to guarantee that execution will *not* continue after such an error. Handling each and every error manually by using the return value requires a lot of discipline, which could be a very steep learning curve for PHP developers used to a fast prototyping language. And if people don't strictly follow the rules, you're opening the door to a whole lot potential bugs in codebases. And, you miss the ability of automatic error reporting for uncaught errors, that you get almost for free with exceptions and a single try/catch at the top level. =E2=80=94 Benjamin On Mon, 21 Oct 2019 at 20:46, Rowan Tommins wrote= : > On 21/10/2019 18:45, Benjamin Morel wrote: > > I personally like exceptions in all cases, as they allow for > > fine-grained error handling, for example: > > > > ``` > > try { > > mkdir('somedir'); > > } catch (FileExistsException $e) { > > // only catch *this* exception, which my program expects, > > // let any other exception (say a PermissionException or a generic > > IoException) bubble up! > > // this *is* what you want: it will be caught by your top-level > > exception handler which will log it to inform you that something is > > wrong with your system > > } > > ``` > > > I think this argument is something of a logical fallacy: just because > exceptions are (or can be) more fine-grained than the current return > value, that doesn't mean they're the best tool for the job. > > If I'm remembering it correctly, an example is the old PEAR > error-handling model, where rather than false, functions returned a > PEAR_Error object. This could be just as fine-grained (using sub-classes > or error code constants) as an exception, but without the additional > behaviour of blowing through the stack. > > In some cases, you can go one step further, and have a "result" object, > with methods like isSuccessful(), getErrorCode(), and getOutput(). > > Importantly, this can and should be different for different functions. > > In a previous message you wrote this, which I think mischaracterizes / > misunderstands previous discussions: > > > > To play the devil's advocate, however, IIRC some discussions in the pas= t > > mentioned that the BC breakage that comes with it is such an issue that > it > > would be preferable to create a brand new API rather than attempting to > fix > > the existing one. > > Nobody ever jumped in to propose such an API, though :-( > > > It's not that we need "a brand new API", singular, it's that some of the > more complex sets of functions should be redesigned, with error handling > considered as part of each design. > > So, a new file-handling API could provide a much richer set of error > codes for its equivalent of fopen(), which might or might not involve > exceptions. It might instead have a file handle object which could > represent closed and errored handles, so that attempting to open a file > would always give the same result type, but would have information about > what failed. > > Meanwhile, if we were to design a new string API to make things more > consistent, and perhaps introduce better Unicode support where relevant, > that would be a good chance to throw exceptions for truly exceptional > cases. However, we might also include more forgiving variants of > functions, for "best effort" processing of user input (like iconv's > //TRANSLIT and //IGNORE modes). > > > On the other hand, there are some errors where the answer to the > question "would it be better to halt processing than continue in an > unknown state?" is "yes", so it's reasonable to introduce an exception, > even though existing programs aren't expecting it. But again, this > should be considered for every case, not as a general rule that all > Warnings should be promoted. > > Regards, > > -- > Rowan Tommins (n=C3=A9 Collins) > [IMSoP] > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > --000000000000e15cd4059571a929--