/interpreter/tags/at2-build060407/src/edu/vub/at/exceptions/InterpreterException.java

http://ambienttalk.googlecode.com/ · Java · 134 lines · 73 code · 20 blank · 41 comment · 12 complexity · 3bb9fc787f5dd4200df1e3fa7abb6dc2 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.ATStripe;
  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. public InterpreterException() {
  49. super();
  50. runtimeStack_ = InvocationStack.captureInvocationStack();
  51. cause_ = null;
  52. }
  53. public InterpreterException(String message, Throwable cause) {
  54. super(message);
  55. // Backport from JDK 1.4 to 1.3
  56. // super(message, cause);
  57. cause_ = cause;
  58. runtimeStack_ = InvocationStack.captureInvocationStack();
  59. }
  60. public InterpreterException(String message) {
  61. super(message);
  62. runtimeStack_ = InvocationStack.captureInvocationStack();
  63. cause_ = null;
  64. }
  65. public InterpreterException(Throwable cause) {
  66. // Backport from JDK 1.4 to 1.3
  67. // super(cause);
  68. cause_ = cause;
  69. runtimeStack_ = InvocationStack.captureInvocationStack();
  70. }
  71. public void printAmbientTalkStackTrace(PrintStream out) {
  72. runtimeStack_.printStackTrace(out);
  73. }
  74. public ATObject getAmbientTalkRepresentation() {
  75. return new NATException(this);
  76. }
  77. public abstract ATStripe getStripeType();
  78. public String getMessage() {
  79. if (cause_ == null) {
  80. return super.getMessage();
  81. } else {
  82. return super.getMessage() + " caused by " + cause_.getMessage();
  83. }
  84. }
  85. /* backport from 1.4 interface to 1.3 */
  86. public Throwable getCause() {
  87. return cause_;
  88. }
  89. public void printStackTrace(PrintStream out) {
  90. if (cause_ == null) {
  91. super.printStackTrace(out);
  92. } else {
  93. super.printStackTrace(out);
  94. out.print(" caused by:");
  95. cause_.printStackTrace(out);
  96. }
  97. }
  98. public void printStackTrace(PrintWriter out) {
  99. if (cause_ == null) {
  100. super.printStackTrace(out);
  101. } else {
  102. super.printStackTrace(out);
  103. out.print(" caused by:");
  104. cause_.printStackTrace(out);
  105. }
  106. }
  107. public String toString() {
  108. if (cause_ == null) {
  109. return super.toString();
  110. } else {
  111. return super.toString() + " caused by " + cause_.toString();
  112. }
  113. }
  114. }