73963
|
1 |
/* Title: Tools/Setup/isabelle/setup/Exn.java
|
|
2 |
Author: Makarius
|
|
3 |
|
|
4 |
Support for exceptions (arbitrary throwables).
|
|
5 |
*/
|
|
6 |
|
|
7 |
package isabelle.setup;
|
|
8 |
|
|
9 |
|
|
10 |
import java.io.IOException;
|
|
11 |
import java.util.LinkedList;
|
|
12 |
import java.util.List;
|
|
13 |
|
|
14 |
|
|
15 |
public class Exn
|
|
16 |
{
|
|
17 |
/* interrupts */
|
|
18 |
|
|
19 |
public static boolean is_interrupt(Throwable exn)
|
|
20 |
{
|
|
21 |
boolean found_interrupt = false;
|
|
22 |
Throwable e = exn;
|
|
23 |
while (!found_interrupt && e != null) {
|
|
24 |
found_interrupt = e instanceof InterruptedException;
|
|
25 |
e = e.getCause();
|
|
26 |
}
|
|
27 |
return found_interrupt;
|
|
28 |
}
|
|
29 |
|
|
30 |
public static int INTERRUPT_RETURN_CODE = 130;
|
|
31 |
|
|
32 |
public static int return_code(Throwable exn, int rc)
|
|
33 |
{
|
|
34 |
return is_interrupt(exn) ? INTERRUPT_RETURN_CODE : rc;
|
|
35 |
}
|
|
36 |
|
|
37 |
|
|
38 |
/* message */
|
|
39 |
|
|
40 |
public static String message(Throwable exn)
|
|
41 |
{
|
|
42 |
String msg = exn.getMessage();
|
|
43 |
|
|
44 |
if (exn.getClass() == RuntimeException.class)
|
|
45 |
{
|
|
46 |
return msg == null || msg.isEmpty() ? "Error" : msg;
|
|
47 |
}
|
|
48 |
else if (exn instanceof IOException)
|
|
49 |
{
|
|
50 |
return msg == null || msg.isEmpty() ? "I/O error" : "I/O error: " + msg;
|
|
51 |
}
|
|
52 |
else if (exn instanceof RuntimeException && !msg.isEmpty()) { return msg; }
|
|
53 |
else if (exn instanceof InterruptedException) { return "Interrupt"; }
|
|
54 |
else { return exn.toString(); }
|
|
55 |
}
|
|
56 |
|
|
57 |
|
|
58 |
/* print */
|
|
59 |
|
|
60 |
public static String trace(Throwable exn)
|
|
61 |
{
|
|
62 |
List<String> list = new LinkedList<String>();
|
|
63 |
for (StackTraceElement elem : exn.getStackTrace()) {
|
|
64 |
list.add(elem.toString());
|
|
65 |
}
|
|
66 |
return Library.cat_lines(list);
|
|
67 |
}
|
|
68 |
|
|
69 |
public static boolean debug()
|
|
70 |
{
|
|
71 |
return System.getProperty("isabelle.debug", "").equals("true");
|
|
72 |
}
|
|
73 |
|
|
74 |
public static String print(Throwable exn)
|
|
75 |
{
|
|
76 |
return debug() ? message(exn) + "\n" + trace(exn) : message(exn);
|
|
77 |
}
|
|
78 |
|
|
79 |
public static String print_error(Throwable exn)
|
|
80 |
{
|
|
81 |
return Library.prefix_lines("*** ", print(exn));
|
|
82 |
}
|
|
83 |
}
|