< :前の番号
^ :番号順リスト
> :次の番号
P :前の記事(スレッド移動)
N :次の記事(スレッド移動)
|<:前のスレッド
>|:次のスレッド
^ :返事先
_:自分への返事
>:同じ返事先を持つ記事(前)
<:同じ返事先を持つ記事(後)
---:分割してスレッド表示、再表示
| :分割して(縦)スレッド表示、再表示
~ :スレッドのフレーム消去
.:インデックス
..:インデックスのインデックス
Issue #10481 has been updated by Robert Klemme.
Alex Boyd wrote:
> I'd like to propose a syntax change: allow boolean "if" and "unless" clauses to follow a rescue statement.
> I propose that the following be allowed:
>
> ~~~
> begin
> ...
> rescue SomeError => e if e.error_code == 1
> ...handle error...
> end
> ~~~
Do you have an idea of the runtime performance impact? I mean, a type check (as done today) is fast and effort cannot change. But with your proposal arbitrary code can be executed as part of the test. If you do that on multiple levels of the call stack the effect on performance may be noticeable.
Also it must be defined how you treat exceptions that are raised inside the test expression. It may be hard to hunt down errors because these exceptions would likely shadow the original exception and it may be hard to find out what content in the original exception triggered the exception in the testing code.
Also it must be made sure that code in "ensure" block is executed under all conditions. That may become more difficult with your change.
----------------------------------------
Feature #10481: Add "if" and "unless" clauses to rescue statements
https://bugs.ruby-lang.org/issues/10481#change-50023
* Author: Alex Boyd
* Status: Open
* Priority: Normal
* Assignee: Yukihiro Matsumoto
* Category:
* Target version:
----------------------------------------
I'd like to propose a syntax change: allow boolean "if" and "unless" clauses to follow a rescue statement.
Consider the following:
~~~
begin
...
rescue SomeError => e
if e.error_code == 1
...handle error...
else
raise
end
end
~~~
This is a fairly common way of dealing with exceptions where some condition above and beyond the exception's type determines whether the exception should be rescued. It's verbose, though, and it's not obvious at first glance exactly what conditions are being rescued, especially if "...handle error..." is more than a few lines long. I propose that the following be allowed:
~~~
begin
...
rescue SomeError => e if e.error_code == 1
...handle error...
end
~~~
"unless" would, of course, be allowed as well:
~~~
begin
...
rescue SomeError => e unless e.error_code == 2
...handle error...
end
~~~
A rescue statement whose boolean condition failed would be treated the same as if the exception being raised didn't match the exception being rescued, and move on to the next rescue statement:
~~~
begin
...
rescue SomeError => e if e.error_code == 1
...handle error code 1...
rescue SomeError => e if e.error_code == 2
...handle error code 2...
end
~~~
And finally, catch-all rescue statements would be allowed as well:
~~~
begin
...
rescue => e if e.message == "some error"
...handle error...
end
~~~
---Files--------------------------------
rescue-conditions.diff (6.76 KB)
rescue-conditions.diff (6.57 KB)
--
https://bugs.ruby-lang.org/