author | wenzelm |
Tue, 24 Jul 2012 14:36:08 +0200 | |
changeset 48476 | 44f56fe01528 |
parent 45673 | cd41e3903fbf |
child 48479 | 819f7a5f3e7f |
permissions | -rw-r--r-- |
34136 | 1 |
/* Title: Pure/General/exn.scala |
45673
cd41e3903fbf
separate compilation of PIDE vs. Pure sources, which enables independent Scala library;
wenzelm
parents:
45667
diff
changeset
|
2 |
Module: PIDE |
34136 | 3 |
Author: Makarius |
4 |
||
43761
e72ba84ae58f
tuned signature -- corresponding to Scala version;
wenzelm
parents:
34313
diff
changeset
|
5 |
Support for exceptions (arbitrary throwables). |
34136 | 6 |
*/ |
7 |
||
8 |
package isabelle |
|
9 |
||
10 |
||
11 |
object Exn |
|
12 |
{ |
|
44158 | 13 |
/* exceptions as values */ |
34136 | 14 |
|
15 |
sealed abstract class Result[A] |
|
43762 | 16 |
case class Res[A](res: A) extends Result[A] |
17 |
case class Exn[A](exn: Throwable) extends Result[A] |
|
34136 | 18 |
|
19 |
def capture[A](e: => A): Result[A] = |
|
20 |
try { Res(e) } |
|
34313
2f890016afab
treat *all* JVM throwables as "exceptions", cf. ML version;
wenzelm
parents:
34300
diff
changeset
|
21 |
catch { case exn: Throwable => Exn[A](exn) } |
34136 | 22 |
|
23 |
def release[A](result: Result[A]): A = |
|
24 |
result match { |
|
25 |
case Res(x) => x |
|
26 |
case Exn(exn) => throw exn |
|
27 |
} |
|
44158 | 28 |
|
29 |
||
30 |
/* message */ |
|
31 |
||
32 |
private val runtime_exception = Class.forName("java.lang.RuntimeException") |
|
33 |
||
34 |
def message(exn: Throwable): String = |
|
48476 | 35 |
if (exn.isInstanceOf[java.io.IOException]) { |
36 |
val msg = exn.getMessage |
|
37 |
if (msg == null) "I/O error" |
|
38 |
else "I/O error: " + msg |
|
39 |
} |
|
40 |
else if (exn.getClass == runtime_exception) { |
|
44158 | 41 |
val msg = exn.getMessage |
42 |
if (msg == null) "Error" else msg |
|
43 |
} |
|
44 |
else exn.toString |
|
34136 | 45 |
} |
46 |