/interpreter/branches/proteus_annotations/src/edu/vub/at/exceptions/InterpreterException.java

http://ambienttalk.googlecode.com/ · 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. */
  28. package edu.vub.at.exceptions;
  29. import edu.vub.at.eval.InvocationStack;
  30. import edu.vub.at.objects.ATObject;
  31. import edu.vub.at.objects.ATTypeTag;
  32. import edu.vub.at.objects.natives.NATException;
  33. import java.io.PrintStream;
  34. import java.io.PrintWriter;
  35. /**
  36. * TODO tvcutsem Shouldn't we parameterize NATExceptions with an ATContext and possibly
  37. * also an ATAbstractGrammar for evaluation errors. This allows the user to inspect
  38. * both which expression was evaluated at exception-raising-time and allows him to inspect
  39. * the context at exception-raising-time.
  40. *
  41. * @author smostinc
  42. */
  43. public abstract class InterpreterException extends Exception {
  44. private static final long serialVersionUID = 511962997881825680L;
  45. // The ambienttalk stack trace of the exception
  46. protected final InvocationStack runtimeStack_;
  47. private final Throwable cause_;
  48. /**
  49. * Default constructor which only captures the AmbientTalk invocation stack.
  50. */
  51. public InterpreterException() {
  52. super();
  53. runtimeStack_ = InvocationStack.captureInvocationStack();
  54. cause_ = null;
  55. }
  56. /**
  57. * Contructor which reports an exception with a given message and an underlying exception.
  58. * This constructor also captures the AmbientTalk invocation stack relating the exception
  59. * to the AmbientTalk code that triggered it.
  60. */
  61. public InterpreterException(String message, Throwable cause) {
  62. super(message);
  63. // Backport from JDK 1.4 to 1.3
  64. // super(message, cause);
  65. cause_ = cause;
  66. runtimeStack_ = InvocationStack.captureInvocationStack();
  67. }
  68. /**
  69. * Constructor which reports an exception with a given message which cannot be related to a
  70. * previously raised exception. This constructor captures the AmbientTalk invocation
  71. * stack to identify which code produced the error.
  72. */
  73. public InterpreterException(String message) {
  74. super(message);
  75. runtimeStack_ = InvocationStack.captureInvocationStack();
  76. cause_ = null;
  77. }
  78. /**
  79. * Constructor which creates a wrapper for an underlying exception. This constructor
  80. * captures the AmbientTalk invocation stack to relate the exception to the AmbientTalk
  81. * that it corresponds to.
  82. */
  83. public InterpreterException(Throwable cause) {
  84. // Backport from JDK 1.4 to 1.3
  85. // super(cause);
  86. cause_ = cause;
  87. runtimeStack_ = InvocationStack.captureInvocationStack();
  88. }
  89. /**
  90. * @param out
  91. */
  92. public void printAmbientTalkStackTrace(PrintStream out) {
  93. runtimeStack_.printStackTrace(out);
  94. }
  95. /**
  96. * Returns an ambienttalk representation of the exception. The returned object is a wrapper
  97. * object which provides access to the exception's message and the AmbientTalk stack trace.
  98. */
  99. public ATObject getAmbientTalkRepresentation() {
  100. return new NATException(this);
  101. }
  102. /**
  103. * Returns the native type tag corresponding to the exception class. The NativeTypeTags class
  104. * defines types for all native data types, including exceptions. For exceptions, these
  105. * types can be used to catch native exceptions and handle them. Therefore every exception
  106. * class should override this abstract method to supply the type equivalent of its class.
  107. */
  108. public abstract ATTypeTag getType();
  109. public String getMessage() {
  110. if (cause_ == null) {
  111. return super.getMessage();
  112. } else {
  113. return super.getMessage() + " caused by " + cause_.getMessage();
  114. }
  115. }
  116. /* backport from 1.4 interface to 1.3 */
  117. /**
  118. * As AmbientTalk targets Java 1.3, Interpreter exceptions need to provide explicit support
  119. * to report the exception that caused them. The default support for such a method was only
  120. * introduced in Java 1.4. This method returns the exception that caused a particular error
  121. * which can possibly be null.
  122. */
  123. public Throwable getCause() {
  124. return cause_;
  125. }
  126. public void printStackTrace(PrintStream out) {
  127. if (cause_ == null) {
  128. super.printStackTrace(out);
  129. } else {
  130. super.printStackTrace(out);
  131. out.print(" caused by:");
  132. cause_.printStackTrace(out);
  133. }
  134. }
  135. public void printStackTrace(PrintWriter out) {
  136. if (cause_ == null) {
  137. super.printStackTrace(out);
  138. } else {
  139. super.printStackTrace(out);
  140. out.print(" caused by:");
  141. cause_.printStackTrace(out);
  142. }
  143. }
  144. public String toString() {
  145. if (cause_ == null) {
  146. return super.toString();
  147. } else {
  148. return super.toString() + " caused by " + cause_.toString();
  149. }
  150. }
  151. }