zsh-workers
 help / color / mirror / code / Atom feed
From: Eric Cook <llua@gmx.com>
To: zsh-workers@zsh.org
Subject: Re: Inconsistent behavior of ERR_EXIT with conditionals
Date: Thu, 10 Nov 2022 23:09:07 -0500	[thread overview]
Message-ID: <9804c915-29ea-1ee1-e8e5-638c76ee4fa4@gmx.com> (raw)
In-Reply-To: <CAGdYchteCWPt_2skasZr3suB6HKxtnT4-ARAjivT1BkeGDdy6A@mail.gmail.com>

On 11/10/22 22:04, Philippe Altherr wrote:

> I'm mainly a Java developer. In Java many functions may throw runtime exceptions. You can catch and handle these but in 99% of the cases you just ignore them because you know (or assume) that they won't be thrown. Doing otherwise would be very impractical 
> because your code would be littered with try/catch statements. Every now and then one of these exceptions that you thought could/should never be thrown is nevertheless thrown for some reason. In that case the exception goes up the call stack and, if no 
> catch is encountered, it kills the program and prints a stack trace to the code that threw the exception. This way it's obvious that something failed. It's also easy to figure out where it happened. And, it ensures that no extra harm is done (by keeping 
> running the program).
> 
> In the world of shell scripts, there are no runtime exceptions but some exit statuses play a similar role. Many UNIX commands and shell functions can potentially return a non-zero exit status but when you use them you often know (or assume) that they 
> won't return a non-zero exit status. If they nevertheless do, the default behavior of shell script is to simply ignore the error. So if "cmd1" fails in "cmd1; cmd2", "cmd2" still gets executed. It's already bad that "cmd1" failed. Running "cmd2" could 
> cause extra harm. It looks much safer to exit right after "cmd1". That's the main reason I run all my scripts with ERR_EXIT enabled.
> 
> In addition to that, ERR_EXIT also makes it easier to notice errors and to figure out where they happened, especially if ERR_EXIT is coupled with code that prints a stack trace. Without ERR_EXIT you may not even notice that something went wrong if no 
> error message was printed (or it got swallowed).
> 
> My impression is that ERR_EXIT is commonly used for these reasons. In fact, whenever I have seen it used, it seemed to be for these reasons. But maybe I should confirm that. I'll ask some of my friends if they use it and with what expectations.

Didn't read the entire thread but the first quoted paragraph seems to be the commonly misguided reason to blindly use set -e; attempting to use a paradigm of a language more familiar to the author, to a different language.
As more and more people start their programming careers in languages with exceptions, exception-less languages like unix shells[1] or more recently go-lang[2] looks pretty antiquated.


> In my opinion, the only downside of that usage of ERR_EXIT is that it's far from foolproof. There are plenty of cases where just enabling ERR_EXIT won't be enough to ensure that the script halts at the first unexpected error.
Correct, it is far from foolproof and it can create additional problems when it unexpectedly unexpected errors and terminate the script for reasons that can confuse the author. [1]

[1] http://mywiki.wooledge.org/BashFAQ/105
[2] https://go.dev/doc/faq#exceptions


  parent reply	other threads:[~2022-11-11  4:09 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-04 16:37 Philippe Altherr
2022-11-06 20:45 ` Bart Schaefer
2022-11-07  3:50   ` Bart Schaefer
2022-11-07  5:35     ` [PATCH] " Bart Schaefer
2022-11-07  9:44       ` Peter Stephenson
2022-11-08  1:20         ` Bart Schaefer
2022-11-08  4:58     ` Philippe Altherr
2022-11-08  5:36       ` Bart Schaefer
2022-11-08  8:04         ` Lawrence Velázquez
2022-11-08 18:51           ` Philippe Altherr
2022-11-08 19:20             ` Lawrence Velázquez
2022-11-08 23:28             ` Bart Schaefer
2022-11-09  4:11               ` Philippe Altherr
2022-11-09  6:00                 ` Bart Schaefer
2022-11-09 14:22                   ` Philippe Altherr
2022-11-10  1:00                     ` Bart Schaefer
2022-11-10  5:09                       ` Bart Schaefer
2022-11-11  3:04                         ` Philippe Altherr
2022-11-11  4:06                           ` Lawrence Velázquez
2022-11-11  4:09                           ` Eric Cook [this message]
2022-11-08 23:11           ` Bart Schaefer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=9804c915-29ea-1ee1-e8e5-638c76ee4fa4@gmx.com \
    --to=llua@gmx.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).