From 006e2f2aadf5d15ff1b9b32f1b7e96960b778933 Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Tue, 10 Dec 2013 00:45:48 +0100 Subject: SI-7912 Be defensive calling `toString` in `MatchError#getMessage` Otherwise, objects with exception-throwing `toString` lead to a cascading error far removed from the originally failed match. --- src/library/scala/MatchError.scala | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'src/library') diff --git a/src/library/scala/MatchError.scala b/src/library/scala/MatchError.scala index 6ba7e833d3..9965bb19b5 100644 --- a/src/library/scala/MatchError.scala +++ b/src/library/scala/MatchError.scala @@ -23,9 +23,15 @@ final class MatchError(obj: Any) extends RuntimeException { /** There's no reason we need to call toString eagerly, * so defer it until getMessage is called. */ - private lazy val objString = + private lazy val objString = { + def ofClass = "of class " + obj.getClass.getName if (obj == null) "null" - else obj.toString() + " (of class " + obj.getClass.getName + ")" + else try { + obj.toString() + " (" + ofClass + ")" + } catch { + case _: Throwable => "an instance " + ofClass + } + } override def getMessage() = objString } -- cgit v1.2.3