/interpreter/tags/at2-build270707/src/edu/vub/at/exceptions/InterpreterException.java
Java | 170 lines | 73 code | 19 blank | 78 comment | 12 complexity | a0a1608892e5ea357b7027c27be73e9e MD5 | raw file
1/** 2 * AmbientTalk/2 Project 3 * ATException.java created on Jul 13, 2006 at 8:24:20 PM 4 * (c) Programming Technology Lab, 2006 - 2007 5 * Authors: Tom Van Cutsem & Stijn Mostinckx 6 * 7 * Permission is hereby granted, free of charge, to any person 8 * obtaining a copy of this software and associated documentation 9 * files (the "Software"), to deal in the Software without 10 * restriction, including without limitation the rights to use, 11 * copy, modify, merge, publish, distribute, sublicense, and/or 12 * sell copies of the Software, and to permit persons to whom the 13 * Software is furnished to do so, subject to the following 14 * conditions: 15 * 16 * The above copyright notice and this permission notice shall be 17 * included in all copies or substantial portions of the Software. 18 * 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 21 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 23 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 24 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 26 * OTHER DEALINGS IN THE SOFTWARE. 27 */ 28package edu.vub.at.exceptions; 29 30import edu.vub.at.eval.InvocationStack; 31import edu.vub.at.objects.ATObject; 32import edu.vub.at.objects.ATTypeTag; 33import edu.vub.at.objects.natives.NATException; 34 35import java.io.PrintStream; 36import java.io.PrintWriter; 37 38/** 39 * TODO tvcutsem Shouldn't we parameterize NATExceptions with an ATContext and possibly 40 * also an ATAbstractGrammar for evaluation errors. This allows the user to inspect 41 * both which expression was evaluated at exception-raising-time and allows him to inspect 42 * the context at exception-raising-time. 43 * 44 * @author smostinc 45 */ 46public abstract class InterpreterException extends Exception { 47 48 private static final long serialVersionUID = 511962997881825680L; 49 50 // The ambienttalk stack trace of the exception 51 protected final InvocationStack runtimeStack_; 52 53 private final Throwable cause_; 54 55 /** 56 * Default constructor which only captures the AmbientTalk invocation stack. 57 */ 58 public InterpreterException() { 59 super(); 60 runtimeStack_ = InvocationStack.captureInvocationStack(); 61 cause_ = null; 62 } 63 64 /** 65 * Contructor which reports an exception with a given message and an underlying exception. 66 * This constructor also captures the AmbientTalk invocation stack relating the exception 67 * to the AmbientTalk code that triggered it. 68 */ 69 public InterpreterException(String message, Throwable cause) { 70 super(message); 71 // Backport from JDK 1.4 to 1.3 72 // super(message, cause); 73 cause_ = cause; 74 runtimeStack_ = InvocationStack.captureInvocationStack(); 75 } 76 77 /** 78 * Constructor which reports an exception with a given message which cannot be related to a 79 * previously raised exception. This constructor captures the AmbientTalk invocation 80 * stack to identify which code produced the error. 81 */ 82 public InterpreterException(String message) { 83 super(message); 84 runtimeStack_ = InvocationStack.captureInvocationStack(); 85 cause_ = null; 86 } 87 88 /** 89 * Constructor which creates a wrapper for an underlying exception. This constructor 90 * captures the AmbientTalk invocation stack to relate the exception to the AmbientTalk 91 * that it corresponds to. 92 */ 93 public InterpreterException(Throwable cause) { 94 // Backport from JDK 1.4 to 1.3 95 // super(cause); 96 cause_ = cause; 97 runtimeStack_ = InvocationStack.captureInvocationStack(); 98 } 99 100 101 /** 102 * @param out 103 */ 104 public void printAmbientTalkStackTrace(PrintStream out) { 105 runtimeStack_.printStackTrace(out); 106 } 107 108 /** 109 * Returns an ambienttalk representation of the exception. The returned object is a wrapper 110 * object which provides access to the exception's message and the AmbientTalk stack trace. 111 */ 112 public ATObject getAmbientTalkRepresentation() { 113 return new NATException(this); 114 } 115 116 /** 117 * Returns the native type tag corresponding to the exception class. The NativeTypeTags class 118 * defines types for all native data types, including exceptions. For exceptions, these 119 * types can be used to catch native exceptions and handle them. Therefore every exception 120 * class should override this abstract method to supply the type equivalent of its class. 121 */ 122 public abstract ATTypeTag getType(); 123 124 public String getMessage() { 125 if (cause_ == null) { 126 return super.getMessage(); 127 } else { 128 return super.getMessage() + " caused by " + cause_.getMessage(); 129 } 130 } 131 132 /* backport from 1.4 interface to 1.3 */ 133 /** 134 * As AmbientTalk targets Java 1.3, Interpreter exceptions need to provide explicit support 135 * to report the exception that caused them. The default support for such a method was only 136 * introduced in Java 1.4. This method returns the exception that caused a particular error 137 * which can possibly be null. 138 */ 139 public Throwable getCause() { 140 return cause_; 141 } 142 143 public void printStackTrace(PrintStream out) { 144 if (cause_ == null) { 145 super.printStackTrace(out); 146 } else { 147 super.printStackTrace(out); 148 out.print(" caused by:"); 149 cause_.printStackTrace(out); 150 } 151 } 152 153 public void printStackTrace(PrintWriter out) { 154 if (cause_ == null) { 155 super.printStackTrace(out); 156 } else { 157 super.printStackTrace(out); 158 out.print(" caused by:"); 159 cause_.printStackTrace(out); 160 } 161 } 162 163 public String toString() { 164 if (cause_ == null) { 165 return super.toString(); 166 } else { 167 return super.toString() + " caused by " + cause_.toString(); 168 } 169 } 170}