1. /*
  2. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  3. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  4. */
  5. package javax.mail;
  6. import java.lang.*;
  7. /**
  8. * The base class for all exceptions thrown by the Messaging classes
  9. *
  10. * @author John Mani
  11. */
  12. public class MessagingException extends Exception {
  13. /**
  14. * The next exception in the chain.
  15. *
  16. * @serial
  17. */
  18. private Exception next;
  19. /**
  20. * Constructs a MessagingException with no detail message.
  21. */
  22. public MessagingException() {
  23. super();
  24. }
  25. /**
  26. * Constructs a MessagingException with the specified detail message.
  27. * @param s the detail message
  28. */
  29. public MessagingException(String s) {
  30. super(s);
  31. }
  32. /**
  33. * Constructs a MessagingException with the specified
  34. * Exception and detail message. The specified exception is chained
  35. * to this exception.
  36. * @param s the detail message
  37. * @param e the embedded exception
  38. * @see #getNextException
  39. * @see #setNextException
  40. */
  41. public MessagingException(String s, Exception e) {
  42. super(s);
  43. next = e;
  44. }
  45. /**
  46. * Get the next exception chained to this one. If the
  47. * next exception is a MessagingException, the chain
  48. * may extend further.
  49. *
  50. * @return next Exception, null if none.
  51. */
  52. public Exception getNextException() {
  53. return next;
  54. }
  55. /**
  56. * Add an exception to the end of the chain. If the end
  57. * is <strong>not</strong> a MessagingException, this
  58. * exception cannot be added to the end.
  59. *
  60. * @param ex the new end of the Exception chain
  61. * @return <code>true</code> if the this Exception
  62. * was added, <code>false</code> otherwise.
  63. */
  64. public synchronized boolean setNextException(Exception ex) {
  65. Exception theEnd = this;
  66. while (theEnd instanceof MessagingException &&
  67. ((MessagingException)theEnd).next != null) {
  68. theEnd = ((MessagingException)theEnd).next;
  69. }
  70. // If the end is a MessagingException, we can add this
  71. // exception to the chain.
  72. if (theEnd instanceof MessagingException) {
  73. ((MessagingException)theEnd).next = ex;
  74. return true;
  75. } else
  76. return false;
  77. }
  78. /**
  79. * Produce the message, include the message from the nested
  80. * exception if there is one.
  81. */
  82. public String getMessage() {
  83. if (next == null)
  84. return super.getMessage();
  85. else
  86. return super.getMessage() +
  87. ";\n nested exception is: \n\t" +
  88. next.toString();
  89. }
  90. }