author | wenzelm |
Tue, 29 May 2018 14:25:39 +0200 | |
changeset 68315 | d088799fd278 |
parent 68131 | 62a3294edda3 |
child 68806 | 4597812d5182 |
permissions | -rw-r--r-- |
62508
d0b68218ea55
discontinued RAW session: bootstrap directly from isabelle_process RAW_ML_SYSTEM;
wenzelm
parents:
62492
diff
changeset
|
1 |
/* Title: Pure/General/exn.scala |
34136 | 2 |
Author: Makarius |
3 |
||
43761
e72ba84ae58f
tuned signature -- corresponding to Scala version;
wenzelm
parents:
34313
diff
changeset
|
4 |
Support for exceptions (arbitrary throwables). |
34136 | 5 |
*/ |
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
10 |
object Exn |
|
11 |
{ |
|
62492 | 12 |
/* user errors */ |
13 |
||
14 |
class User_Error(message: String) extends RuntimeException(message) |
|
15 |
{ |
|
16 |
override def equals(that: Any): Boolean = |
|
17 |
that match { |
|
18 |
case other: User_Error => message == other.getMessage |
|
19 |
case _ => false |
|
20 |
} |
|
21 |
override def hashCode: Int = message.hashCode |
|
22 |
||
65828 | 23 |
override def toString: String = "\n" + Output.error_message_text(message) |
62492 | 24 |
} |
25 |
||
26 |
object ERROR |
|
27 |
{ |
|
28 |
def apply(message: String): User_Error = new User_Error(message) |
|
29 |
def unapply(exn: Throwable): Option[String] = user_message(exn) |
|
30 |
} |
|
31 |
||
32 |
def error(message: String): Nothing = throw ERROR(message) |
|
33 |
||
68131 | 34 |
def cat_message(msgs: String*): String = |
35 |
cat_lines(msgs.iterator.filterNot(_ == "")) |
|
62492 | 36 |
|
68131 | 37 |
def cat_error(msgs: String*): Nothing = |
38 |
error(cat_message(msgs:_*)) |
|
62492 | 39 |
|
40 |
||
44158 | 41 |
/* exceptions as values */ |
34136 | 42 |
|
43 |
sealed abstract class Result[A] |
|
59698
d4ce901f20c5
value-oriented user error, for well-defined Thy_Syntax.chop_common;
wenzelm
parents:
59697
diff
changeset
|
44 |
{ |
d4ce901f20c5
value-oriented user error, for well-defined Thy_Syntax.chop_common;
wenzelm
parents:
59697
diff
changeset
|
45 |
def user_error: Result[A] = |
d4ce901f20c5
value-oriented user error, for well-defined Thy_Syntax.chop_common;
wenzelm
parents:
59697
diff
changeset
|
46 |
this match { |
d4ce901f20c5
value-oriented user error, for well-defined Thy_Syntax.chop_common;
wenzelm
parents:
59697
diff
changeset
|
47 |
case Exn(ERROR(msg)) => Exn(ERROR(msg)) |
d4ce901f20c5
value-oriented user error, for well-defined Thy_Syntax.chop_common;
wenzelm
parents:
59697
diff
changeset
|
48 |
case _ => this |
d4ce901f20c5
value-oriented user error, for well-defined Thy_Syntax.chop_common;
wenzelm
parents:
59697
diff
changeset
|
49 |
} |
d4ce901f20c5
value-oriented user error, for well-defined Thy_Syntax.chop_common;
wenzelm
parents:
59697
diff
changeset
|
50 |
} |
43762 | 51 |
case class Res[A](res: A) extends Result[A] |
52 |
case class Exn[A](exn: Throwable) extends Result[A] |
|
34136 | 53 |
|
54 |
def capture[A](e: => A): Result[A] = |
|
55 |
try { Res(e) } |
|
34313
2f890016afab
treat *all* JVM throwables as "exceptions", cf. ML version;
wenzelm
parents:
34300
diff
changeset
|
56 |
catch { case exn: Throwable => Exn[A](exn) } |
34136 | 57 |
|
58 |
def release[A](result: Result[A]): A = |
|
59 |
result match { |
|
60 |
case Res(x) => x |
|
61 |
case Exn(exn) => throw exn |
|
62 |
} |
|
44158 | 63 |
|
59136
c2b23cb8a677
added Par_List in Scala, in accordance to ML version;
wenzelm
parents:
57831
diff
changeset
|
64 |
def release_first[A](results: List[Result[A]]): List[A] = |
59138 | 65 |
results.find({ case Exn(exn) => !is_interrupt(exn) case _ => false }) match { |
66 |
case Some(Exn(exn)) => throw exn |
|
67 |
case _ => results.map(release(_)) |
|
68 |
} |
|
59136
c2b23cb8a677
added Par_List in Scala, in accordance to ML version;
wenzelm
parents:
57831
diff
changeset
|
69 |
|
44158 | 70 |
|
56667
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
52066
diff
changeset
|
71 |
/* interrupts */ |
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
52066
diff
changeset
|
72 |
|
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
52066
diff
changeset
|
73 |
def is_interrupt(exn: Throwable): Boolean = |
56670 | 74 |
{ |
75 |
var found_interrupt = false |
|
76 |
var e = exn |
|
77 |
while (!found_interrupt && e != null) { |
|
78 |
found_interrupt |= e.isInstanceOf[InterruptedException] |
|
79 |
e = e.getCause |
|
80 |
} |
|
81 |
found_interrupt |
|
82 |
} |
|
56667
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
52066
diff
changeset
|
83 |
|
68315 | 84 |
def interruptible_capture[A](e: => A): Result[A] = |
85 |
try { Res(e) } |
|
86 |
catch { case exn: Throwable if !is_interrupt(exn) => Exn[A](exn) } |
|
87 |
||
56667
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
52066
diff
changeset
|
88 |
object Interrupt |
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
52066
diff
changeset
|
89 |
{ |
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
52066
diff
changeset
|
90 |
def apply(): Throwable = new InterruptedException |
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
52066
diff
changeset
|
91 |
def unapply(exn: Throwable): Boolean = is_interrupt(exn) |
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
52066
diff
changeset
|
92 |
|
61558 | 93 |
def expose() { if (Thread.interrupted) throw apply() } |
56861 | 94 |
def impose() { Thread.currentThread.interrupt } |
56860 | 95 |
|
56862
e6f7ed54d64e
more robust process kill -- postpone interrupts on current thread;
wenzelm
parents:
56861
diff
changeset
|
96 |
def postpone[A](body: => A): Option[A] = |
e6f7ed54d64e
more robust process kill -- postpone interrupts on current thread;
wenzelm
parents:
56861
diff
changeset
|
97 |
{ |
e6f7ed54d64e
more robust process kill -- postpone interrupts on current thread;
wenzelm
parents:
56861
diff
changeset
|
98 |
val interrupted = Thread.interrupted |
e6f7ed54d64e
more robust process kill -- postpone interrupts on current thread;
wenzelm
parents:
56861
diff
changeset
|
99 |
val result = capture { body } |
e6f7ed54d64e
more robust process kill -- postpone interrupts on current thread;
wenzelm
parents:
56861
diff
changeset
|
100 |
if (interrupted) impose() |
e6f7ed54d64e
more robust process kill -- postpone interrupts on current thread;
wenzelm
parents:
56861
diff
changeset
|
101 |
result match { |
e6f7ed54d64e
more robust process kill -- postpone interrupts on current thread;
wenzelm
parents:
56861
diff
changeset
|
102 |
case Res(x) => Some(x) |
e6f7ed54d64e
more robust process kill -- postpone interrupts on current thread;
wenzelm
parents:
56861
diff
changeset
|
103 |
case Exn(e) => |
e6f7ed54d64e
more robust process kill -- postpone interrupts on current thread;
wenzelm
parents:
56861
diff
changeset
|
104 |
if (is_interrupt(e)) { impose(); None } |
e6f7ed54d64e
more robust process kill -- postpone interrupts on current thread;
wenzelm
parents:
56861
diff
changeset
|
105 |
else throw e |
e6f7ed54d64e
more robust process kill -- postpone interrupts on current thread;
wenzelm
parents:
56861
diff
changeset
|
106 |
} |
e6f7ed54d64e
more robust process kill -- postpone interrupts on current thread;
wenzelm
parents:
56861
diff
changeset
|
107 |
} |
e6f7ed54d64e
more robust process kill -- postpone interrupts on current thread;
wenzelm
parents:
56861
diff
changeset
|
108 |
|
56667
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
52066
diff
changeset
|
109 |
val return_code = 130 |
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
52066
diff
changeset
|
110 |
} |
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
52066
diff
changeset
|
111 |
|
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
52066
diff
changeset
|
112 |
|
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
52066
diff
changeset
|
113 |
/* POSIX return code */ |
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
52066
diff
changeset
|
114 |
|
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
52066
diff
changeset
|
115 |
def return_code(exn: Throwable, rc: Int): Int = |
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
52066
diff
changeset
|
116 |
if (is_interrupt(exn)) Interrupt.return_code else rc |
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
52066
diff
changeset
|
117 |
|
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
52066
diff
changeset
|
118 |
|
44158 | 119 |
/* message */ |
120 |
||
48479
819f7a5f3e7f
more general notion of user ERROR (cf. 44f56fe01528);
wenzelm
parents:
48476
diff
changeset
|
121 |
def user_message(exn: Throwable): Option[String] = |
59697
43e14b0e2ef8
more explicit exception User_Error, with value-oriented equality;
wenzelm
parents:
59138
diff
changeset
|
122 |
if (exn.getClass == classOf[RuntimeException] || |
62492 | 123 |
exn.getClass == classOf[User_Error]) |
59697
43e14b0e2ef8
more explicit exception User_Error, with value-oriented equality;
wenzelm
parents:
59138
diff
changeset
|
124 |
{ |
65716 | 125 |
Some(proper_string(exn.getMessage) getOrElse "Error") |
44158 | 126 |
} |
63782 | 127 |
else if (exn.isInstanceOf[java.sql.SQLException]) |
128 |
{ |
|
65716 | 129 |
Some(proper_string(exn.getMessage) getOrElse "SQL error") |
63782 | 130 |
} |
59697
43e14b0e2ef8
more explicit exception User_Error, with value-oriented equality;
wenzelm
parents:
59138
diff
changeset
|
131 |
else if (exn.isInstanceOf[java.io.IOException]) |
43e14b0e2ef8
more explicit exception User_Error, with value-oriented equality;
wenzelm
parents:
59138
diff
changeset
|
132 |
{ |
43e14b0e2ef8
more explicit exception User_Error, with value-oriented equality;
wenzelm
parents:
59138
diff
changeset
|
133 |
val msg = exn.getMessage |
43e14b0e2ef8
more explicit exception User_Error, with value-oriented equality;
wenzelm
parents:
59138
diff
changeset
|
134 |
Some(if (msg == null || msg == "") "I/O error" else "I/O error: " + msg) |
43e14b0e2ef8
more explicit exception User_Error, with value-oriented equality;
wenzelm
parents:
59138
diff
changeset
|
135 |
} |
48479
819f7a5f3e7f
more general notion of user ERROR (cf. 44f56fe01528);
wenzelm
parents:
48476
diff
changeset
|
136 |
else if (exn.isInstanceOf[RuntimeException]) Some(exn.toString) |
819f7a5f3e7f
more general notion of user ERROR (cf. 44f56fe01528);
wenzelm
parents:
48476
diff
changeset
|
137 |
else None |
819f7a5f3e7f
more general notion of user ERROR (cf. 44f56fe01528);
wenzelm
parents:
48476
diff
changeset
|
138 |
|
819f7a5f3e7f
more general notion of user ERROR (cf. 44f56fe01528);
wenzelm
parents:
48476
diff
changeset
|
139 |
def message(exn: Throwable): String = |
56667
65e84b0ef974
more abstract Exn.Interrupt and POSIX return code;
wenzelm
parents:
52066
diff
changeset
|
140 |
user_message(exn) getOrElse (if (is_interrupt(exn)) "Interrupt" else exn.toString) |
34136 | 141 |
} |