-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
MatchError.scala
40 lines (36 loc) · 1.02 KB
/
MatchError.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
* Scala (https://www.scala-lang.org)
*
* Copyright EPFL and Lightbend, Inc.
*
* Licensed under Apache License 2.0
* (http://www.apache.org/licenses/LICENSE-2.0).
*
* See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*/
package scala
/** This class implements errors which are thrown whenever an
* object doesn't match any pattern of a pattern matching
* expression.
*/
final class MatchError(@transient obj: Any) extends RuntimeException {
/** There's no reason we need to call toString eagerly,
* so defer it until getMessage is called or object is serialized
*/
private[this] lazy val objString = {
def ofClass = "of class " + obj.getClass.getName
if (obj == null) "null"
else
try s"$obj ($ofClass)"
catch {
case _: Throwable => "an instance " + ofClass
}
}
@throws[java.io.ObjectStreamException]
private def writeReplace(): Object = {
objString
this
}
override def getMessage() = objString
}