1. /*
  2. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  3. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  4. */
  5. package javax.ejb;
  6. /**
  7. * The EJBException exception is thrown by an enterprise Bean instance to
  8. * its container to report that the invoked business method or callback method
  9. * could not be completed because of an unexpected error (e.g. the instance
  10. * failed to open a database connection).
  11. */
  12. public class EJBException extends java.lang.RuntimeException {
  13. /**
  14. * @serial
  15. */
  16. private Exception causeException = null;
  17. /**
  18. * Constructs an EJBException with no detail message.
  19. */
  20. public EJBException() {
  21. }
  22. /**
  23. * Constructs an EJBException with the specified
  24. * detailed message.
  25. */
  26. public EJBException(String message) {
  27. super(message);
  28. }
  29. /**
  30. * Constructs an EJBException that embeds the originally thrown exception.
  31. */
  32. public EJBException(Exception ex) {
  33. super();
  34. causeException = ex;
  35. }
  36. /**
  37. * Constructs an EJBException that embeds the originally thrown exception
  38. * with the specified detail message.
  39. */
  40. public EJBException(String message, Exception ex) {
  41. super(message);
  42. causeException = ex;
  43. }
  44. /**
  45. * Obtain the exception that caused the EJBException being thrown.
  46. */
  47. public Exception getCausedByException() {
  48. return causeException;
  49. }
  50. /**
  51. * Returns the detail message, including the message from the nested
  52. * exception if there is one.
  53. */
  54. public String getMessage() {
  55. String msg = super.getMessage();
  56. if (causeException == null)
  57. return msg;
  58. else if ( msg == null ) {
  59. return "nested exception is: " + causeException.toString();
  60. }
  61. else {
  62. return msg + "; nested exception is: " + causeException.toString();
  63. }
  64. }
  65. /**
  66. * Prints the composite message and the embedded stack trace to
  67. * the specified stream <code>ps</code>.
  68. * @param ps the print stream
  69. */
  70. public void printStackTrace(java.io.PrintStream ps)
  71. {
  72. if (causeException == null) {
  73. super.printStackTrace(ps);
  74. } else {
  75. synchronized(ps) {
  76. ps.println(this);
  77. // Print the cause exception first, so that the output
  78. // appears in stack order (i.e. innermost exception first)
  79. causeException.printStackTrace(ps);
  80. super.printStackTrace(ps);
  81. }
  82. }
  83. }
  84. /**
  85. * Prints the composite message to <code>System.err</code>.
  86. */
  87. public void printStackTrace()
  88. {
  89. printStackTrace(System.err);
  90. }
  91. /**
  92. * Prints the composite message and the embedded stack trace to
  93. * the specified print writer <code>pw</code>.
  94. * @param pw the print writer
  95. */
  96. public void printStackTrace(java.io.PrintWriter pw)
  97. {
  98. if (causeException == null) {
  99. super.printStackTrace(pw);
  100. } else {
  101. synchronized(pw) {
  102. pw.println(this);
  103. // Print the cause exception first, so that the output
  104. // appears in stack order (i.e. innermost exception first)
  105. causeException.printStackTrace(pw);
  106. super.printStackTrace(pw);
  107. }
  108. }
  109. }
  110. }