Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 07b497b

Browse files
author
José Valim
committed
Update rescue examples
1 parent 6be8c79 commit 07b497b

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

getting_started/2.markdown

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -565,17 +565,19 @@ The common strategy then is to make explicit all arguments that are required aft
565565

566566
### 2.7.7 Rescue
567567

568-
While `catch` clauses inside `try` are simply a pattern matching mechanism, `rescue` provides a higher abstraction around exceptions. `rescue` allows a developer to rescue an exception by its name and not by its internal contents. Consider the following examples:
568+
For catching exceptions in Elixir, we can use `rescue` instead of `catch`. Besides allowing the same pattern matching rules as `catch`, `rescue` also allows a developer to easily rescue an exception by its name and not by its internal contents. Consider the following examples:
569569

570+
# rescue only runtime error
570571
try do
571572
raise "some error"
572573
rescue: RuntimeError
573574
"rescued"
574575
end
575576

577+
# rescue runtime and argument errors
576578
try do
577579
raise "some error"
578-
rescue: [RuntimeError]
580+
rescue: [RuntimeError, ArgumentError]
579581
"rescued"
580582
end
581583

@@ -590,7 +592,7 @@ While `catch` clauses inside `try` are simply a pattern matching mechanism, `res
590592
# rescue all (discouraged) and assign to x
591593
try do
592594
raise ArgumentError, message: "unexpected argument"
593-
rescue: x in _
595+
rescue: x
594596
x.message
595597
end
596598

0 commit comments

Comments
 (0)