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 all 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
78 changes: 78 additions & 0 deletions tests/run/rescue.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import scala.util.control.NonFatal
import scala.util.control.NonLocalReturns._

object lib {
inline def (op: => T) rescue[T] (fallback: => T) =
try op
catch {
case NonFatal(_) => fallback // ReturnThrowable is fatal error, thus ignored
}

inline def (op: => T) rescue[T, E <: Throwable] (fallback: PartialFunction[E, T]) =
try op
catch {
// case ex: ReturnThrowable[_] => throw ex // bug #7041
case ex: E =>
// user should never match `ReturnThrowable`, which breaks semantics of non-local return
if (fallback.isDefinedAt(ex) && !ex.isInstanceOf[ReturnThrowable[_]]) fallback(ex) else throw ex
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe E should b a subtype of Exception to avoid catching other Throwables like ReturnThrowable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is open to discussion --- this is the case where users specify handled exceptions explicitly, but indeed ReturnThrowable should never be handled by user programs.

}
}

import lib._

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

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

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

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

assert(foo(10) == 40)
assert(bar(10) == 40)

// should not catch fatal errors
assert(
try { { throw new OutOfMemoryError(); true } rescue false }
catch { case _: OutOfMemoryError => true }
)

// should catch any errors specified, including fatal errors
assert(
try { { throw new OutOfMemoryError(); true } rescue { case _: OutOfMemoryError => true } }
catch { case _: OutOfMemoryError => false }
)

// should not catch NonLocalReturns
def foo(x: Int): Int = returning[Int] {
{ throwReturn[Int](4 * x) : Int } rescue 10
}

// should catch specified exceptions, but not NonLocalReturn
def bar(x: Int): Int = returning[Int] {
{ throwReturn[Int](4 * x) : Int } rescue { case _ => 10 }
}

}