Skip to content

Add rescue to support streamline error handling #7038

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 27, 2019
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions tests/run/rescue.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
object lib {
inline def (op: => T) rescue[T] (fallback: => T) = try op catch { case _: Throwable => fallback }
inline def (op: => T) rescue[T, E <: Throwable] (fallback: E => T) = try op catch { case ex: E => fallback(ex) }
}

import lib._

@main def Test = {
assert((9 / 1 rescue 1) == 9)
assert((9 / 0 rescue 1) == 1)
assert(((9 / 0 rescue { ex: NullPointerException => 5 }) rescue 10) == 10)
assert(((9 / 0 rescue { ex: ArithmeticException => 5 }) rescue 10) == 5)

assert((9 / 0 rescue {
case ex: NullPointerException => 4
case ex: ArithmeticException => 3
}) == 3)
}