From: Austin Ziegler Date: 2019-06-21T14:54:12-04:00 Subject: [ruby-core:93304] Re: [Ruby trunk Feature#15936] on_error in lieu of rescue, raise --===============0324901328== Content-Type: multipart/alternative; boundary="000000000000fdb5bc058bd9fd91" --000000000000fdb5bc058bd9fd91 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable No, the `ensure` line is `job.fail! if $!`. That adds a conditional to the `ensure`. On Thu, Jun 20, 2019 at 3:17 PM wrote: > Issue #15936 has been updated by kylemacey (Kyle Macey). > > > jeremyevans0 (Jeremy Evans) wrote: > > kylemacey (Kyle Macey) wrote: > > > What would be a thought on using another keyword that doesn't actuall= y > _rescue_ an exception, but performs an operation in the event of an error= ? > Similar to `ensure`, but only in the event of an error. > > > > > > ``` > > > begin > > > some_method > > > on_error StandardError > > > job.fail! > > > end > > > ``` > > > > Thankfully, Ruby already supports what you want: > > > > ```ruby > > begin > > some_method > > ensure > > job.fail! if $! # or use case $! if you want to handle specific > exception classes differently > > end > > ``` > > > > As you can already accomplish this with current Ruby syntax, I do not > think adding a keyword for it is warranted. > > `ensure` fires even if there is no exception. This keyword would only fir= e > when there is an exception. In your example, `job.fail!` would always be > called. > > ---------------------------------------- > Feature #15936: on_error in lieu of rescue, raise > https://bugs.ruby-lang.org/issues/15936#change-78754 > > * Author: kylemacey (Kyle Macey) > * Status: Open > * Priority: Normal > * Assignee: > * Target version: > ---------------------------------------- > A common bad pattern in ruby is to rescue any exception and accidentally > clobber the exception. > > ``` > begin > some_method > rescue StandardError > # > end > ``` > > Most linters will complain if you write rescues like the code above. > However, this could be useful if we want to perform an operation on _any_ > error, as long as we re-raise the exception after doing our work. > > ``` > begin > some_method > rescue StandardError > job.fail! > raise > end > ``` > > Here, though, we run the risk of potentially forgetting to reraise the > exception, or having to make exceptions in our linter for an operation th= at > is overall benign. > > What would be a thought on using another keyword that doesn't actually > _rescue_ an exception, but performs an operation in the event of an error= ? > Similar to `ensure`, but only in the event of an error. > > ``` > begin > some_method > on_error StandardError > job.fail! > end > ``` > > (obviously, someone more creative than me should come up with a better > name) > > > > -- > https://bugs.ruby-lang.org/ > > Unsubscribe: > > --=20 Austin Ziegler =E2=80=A2 halostatue@gmail.com =E2=80=A2 austin@halostatue.c= a http://www.halostatue.ca/ =E2=80=A2 http://twitter.com/halostatue --000000000000fdb5bc058bd9fd91 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
No, the `ensure` line is `job.fail! if $!`. That adds a co= nditional to the `ensure`.

On Thu, Jun 20, 2019 at 3:17 PM <shout@kylemacey.com> wrote:
Issue #15936 has been updated by kylemacey (Kyle Macey).


jeremyevans0 (Jeremy Evans) wrote:
> kylemacey (Kyle Macey) wrote:
> > What would be a thought on using another keyword that doesn't= actually _rescue_ an exception, but performs an operation in the event of = an error? Similar to `ensure`, but only in the event of an error.
> >
> > ```
> > begin
> >=C2=A0 =C2=A0some_method
> > on_error StandardError
> >=C2=A0 =C2=A0job.fail!
> > end
> > ```
>
> Thankfully, Ruby already supports what you want:
>
> ```ruby
> begin
>=C2=A0 =C2=A0some_method
> ensure
>=C2=A0 =C2=A0job.fail! if $! # or use case $! if you want to handle spe= cific exception classes differently
> end
> ```
>
> As you can already accomplish this with current Ruby syntax, I do not = think adding a keyword for it is warranted.

`ensure` fires even if there is no exception. This keyword would only fire = when there is an exception. In your example, `job.fail!` would always be ca= lled.

----------------------------------------
Feature #15936: on_error in lieu of rescue, raise
https://bugs.ruby-lang.org/issues/15936#change-7= 8754

* Author: kylemacey (Kyle Macey)
* Status: Open
* Priority: Normal
* Assignee:
* Target version:
----------------------------------------
A common bad pattern in ruby is to rescue any exception and accidentally cl= obber the exception.

```
begin
=C2=A0 some_method
rescue StandardError
=C2=A0 #
end
```

Most linters will complain if you write rescues like the code above. Howeve= r, this could be useful if we want to perform an operation on _any_ error, = as long as we re-raise the exception after doing our work.

```
begin
=C2=A0 some_method
rescue StandardError
=C2=A0 job.fail!
=C2=A0 raise
end
```

Here, though, we run the risk of potentially forgetting to reraise the exce= ption, or having to make exceptions in our linter for an operation that is = overall benign.

What would be a thought on using another keyword that doesn't actually = _rescue_ an exception, but performs an operation in the event of an error? = Similar to `ensure`, but only in the event of an error.

```
begin
=C2=A0 some_method
on_error StandardError
=C2=A0 job.fail!
end
```

(obviously, someone more creative than me should come up with a better name= )



--
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=3Dunsubscribe= >
<http://lists.ruby-lang.org/cgi-bin/m= ailman/options/ruby-core>


--
--000000000000fdb5bc058bd9fd91-- --===============0324901328== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline Unsubscribe: --===============0324901328==--