Mercurial > hg > truffle
comparison graal/com.oracle.max.base/src/com/sun/max/program/ProgramError.java @ 3733:e233f5660da4
Added Java files from Maxine project.
author | Thomas Wuerthinger <thomas.wuerthinger@oracle.com> |
---|---|
date | Sat, 17 Dec 2011 19:59:18 +0100 |
parents | |
children | bc8527f3071c |
comparison
equal
deleted
inserted
replaced
3732:3e2e8b8abdaf | 3733:e233f5660da4 |
---|---|
1 /* | |
2 * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved. | |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
20 * or visit www.oracle.com if you need additional information or have any | |
21 * questions. | |
22 */ | |
23 package com.sun.max.program; | |
24 | |
25 /** | |
26 * A collection of static methods for reporting errors indicating some fatal condition. | |
27 * In the target VM, these errors can be made to perform a hard exit of the VM by | |
28 * redirecting them via a {@linkplain #setHandler(Handler) registered} error handler. | |
29 */ | |
30 | |
31 public final class ProgramError extends Error { | |
32 | |
33 /** | |
34 * Implemented by a client that can {@linkplain ProgramError#setHandler(Handler) register} | |
35 * itself to handle program errors instead of having them result in a {@link ProgramError} | |
36 * instance raised. | |
37 */ | |
38 public interface Handler { | |
39 | |
40 /** | |
41 * Handles a given error condition. This method should never return normally. | |
42 * | |
43 * @param message a message describing the error condition. This value may be {@code null} | |
44 * @param throwable an exception given more detail on the cause of the error condition. This value may be {@code null} | |
45 */ | |
46 void handle(String message, Throwable throwable); | |
47 } | |
48 | |
49 /** | |
50 * Registers a handler to which error reporting is redirected. Any previously registered handler | |
51 * is overwritten and discarded. | |
52 * | |
53 * @param h if non-null, this object's {@link Handler#handle(String, Throwable)} method is messaged instead of | |
54 * raising a ProgramError. | |
55 */ | |
56 public static void setHandler(Handler h) { | |
57 handler = h; | |
58 } | |
59 | |
60 private ProgramError(String message, Throwable cause) { | |
61 super(message, cause); | |
62 } | |
63 | |
64 /** | |
65 * Checks a given condition and if it's {@code false}, the appropriate error handling action is taken. | |
66 * The message reported to this action is {@code "Program Error"}. | |
67 * | |
68 * @param condition a condition to test | |
69 */ | |
70 public static void check(boolean condition) { | |
71 if (!condition) { | |
72 throw unexpected(null, null); | |
73 } | |
74 } | |
75 | |
76 /** | |
77 * Checks a given condition and if it's {@code false}, the appropriate error handling action is taken. | |
78 * | |
79 * @param condition a condition to test | |
80 * @param message a message describing the error condition being tested | |
81 */ | |
82 public static void check(boolean condition, String message) { | |
83 if (!condition) { | |
84 throw unexpected(message, null); | |
85 } | |
86 } | |
87 | |
88 /** | |
89 * Checks a given condition and if it's {@code false}, the appropriate error handling action is taken. | |
90 * | |
91 * @param condition a condition to test | |
92 * @param message a message describing the error condition being tested | |
93 * @param object an object whose string description is to be appended to the message | |
94 */ | |
95 public static void check(boolean condition, String message, Object object) { | |
96 if (!condition) { | |
97 throw unexpected(message + object.toString(), null); | |
98 } | |
99 } | |
100 | |
101 /** | |
102 * Reports the occurrence of some error condition triggering the appropriate error handling action | |
103 * to be taken. By default, this action is to raise a {@link ProgramError} exception. However, | |
104 * if an alternative error handler has been {@linkplain #setHandler(Handler) registered}, its | |
105 * {@link Handler#handle(String, Throwable)} method is called instead. | |
106 * | |
107 * This method never returns normally. | |
108 * | |
109 * @param message a message describing the error condition. This value may be {@code null}. | |
110 * @param throwable an exception given more detail on the cause of the error condition. This value may be {@code null}. | |
111 */ | |
112 public static ProgramError unexpected(String message, Throwable throwable) { | |
113 if (handler != null) { | |
114 handler.handle(message, throwable); | |
115 } | |
116 if (message == null) { | |
117 throw new ProgramError("Unexpected Program Error:", throwable); | |
118 } | |
119 throw new ProgramError("Unexpected Program Error: " + message, throwable); | |
120 } | |
121 | |
122 /** | |
123 * Reports the occurrence of some error condition. | |
124 * | |
125 * This method never returns normally. | |
126 * | |
127 * @param message a message describing the error condition. This value may be {@code null}. | |
128 * @see #unexpected(String, Throwable) | |
129 */ | |
130 public static ProgramError unexpected(String message) { | |
131 throw unexpected(message, null); | |
132 } | |
133 | |
134 /** | |
135 * Reports the occurrence of some error condition. | |
136 * | |
137 * This method never returns normally. | |
138 * | |
139 * @param throwable an exception given more detail on the cause of the error condition. This value may be {@code null} | |
140 * @see #unexpected(String, Throwable) | |
141 */ | |
142 public static ProgramError unexpected(Throwable throwable) { | |
143 throw unexpected(null, throwable); | |
144 } | |
145 | |
146 /** | |
147 * Reports the occurrence of some error condition. | |
148 * | |
149 * This method never returns normally. | |
150 * | |
151 * @param throwable an exception given more detail on the cause of the error condition. This value may be {@code null} | |
152 * @see #unexpected(String, Throwable) | |
153 */ | |
154 public static ProgramError unexpected() { | |
155 throw unexpected((String) null); | |
156 } | |
157 | |
158 /** | |
159 * Reports that a {@code switch} statement encountered a {@code case} value it was not expecting. | |
160 * | |
161 * This method never returns normally. | |
162 * | |
163 * @see #unexpected(String, Throwable) | |
164 */ | |
165 public static ProgramError unknownCase() { | |
166 throw unexpected("unknown switch case"); | |
167 } | |
168 | |
169 /** | |
170 * Reports that a {@code switch} statement encountered a {@code case} value it was not expecting. | |
171 * | |
172 * This method never returns normally. | |
173 * | |
174 * @param caseValue the unexpected {@code case} value as a string | |
175 * @see #unexpected(String, Throwable) | |
176 */ | |
177 public static ProgramError unknownCase(String caseValue) { | |
178 throw unexpected("unknown switch case: " + caseValue); | |
179 } | |
180 | |
181 // Checkstyle: stop | |
182 private static Handler handler = null; | |
183 } |