1. /*
  2. * @(#)PrivilegedActionException.java 1.9 00/04/06
  3. *
  4. * Copyright 1998-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.security;
  11. import java.io.PrintWriter;
  12. import java.io.PrintStream;
  13. /**
  14. * This exception is thrown by
  15. * <code>doPrivileged(PrivilegedExceptionAction)</code> and
  16. * <code>doPrivileged(PrivilegedExceptionAction,
  17. * AccessControlContext context)</code> to indicate
  18. * that the action being performed threw a checked exception. The exception
  19. * thrown by the action can be obtained by calling the
  20. * <code>getException</code> method. In effect, an
  21. * <code>PrivilegedActionException</code> is a "wrapper"
  22. * for an exception thrown by a privileged action.
  23. *
  24. * @see PrivilegedExceptionAction
  25. * @see AccessController#doPrivileged(PrivilegedExceptionAction)
  26. * @see AccessController#doPrivileged(PrivilegedExceptionAction,AccessControlContext)
  27. */
  28. public
  29. class PrivilegedActionException extends Exception {
  30. // use serialVersionUID from JDK 1.2.2 for interoperability
  31. private static final long serialVersionUID = 4724086851538908602L;
  32. /**
  33. * @serial
  34. */
  35. private Exception exception;
  36. /**
  37. * Constructs a new PrivilegedActionException "wrapping"
  38. * the specific Exception.
  39. *
  40. * @param exception The exception thrown
  41. */
  42. public PrivilegedActionException(Exception exception) {
  43. this.exception = exception;
  44. }
  45. /**
  46. * Returns the exception thrown by the privileged computation that
  47. * resulted in this <code>PrivilegedActionException</code>.
  48. *
  49. * @return the exception thrown by the privileged computation that
  50. * resulted in this <code>PrivilegedActionException</code>.
  51. * @see PrivilegedExceptionAction
  52. * @see AccessController#doPrivileged(PrivilegedExceptionAction)
  53. * @see AccessController#doPrivileged(PrivilegedExceptionAction,
  54. * AccessControlContext)
  55. */
  56. public Exception getException() {
  57. return exception;
  58. }
  59. /**
  60. * Prints the stack trace of the exception that occurred.
  61. *
  62. * @see java.lang.System#err
  63. */
  64. public void printStackTrace() {
  65. printStackTrace(System.err);
  66. }
  67. /**
  68. * Prints the stack trace of the exception that occurred to the
  69. * specified print stream.
  70. */
  71. public void printStackTrace(PrintStream ps) {
  72. synchronized (ps) {
  73. if (exception != null) {
  74. ps.print("java.security.PrivilegedActionException: ");
  75. exception.printStackTrace(ps);
  76. } else {
  77. super.printStackTrace(ps);
  78. }
  79. }
  80. }
  81. /**
  82. * Prints the stack trace of the exception that occurred to the
  83. * specified print writer.
  84. */
  85. public void printStackTrace(PrintWriter pw) {
  86. synchronized (pw) {
  87. if (exception != null) {
  88. pw.print("java.security.PrivilegedActionException: ");
  89. exception.printStackTrace(pw);
  90. } else {
  91. super.printStackTrace(pw);
  92. }
  93. }
  94. }
  95. /**
  96. * Returns a string describing this exception, including a description
  97. * of the exception it wraps.
  98. *
  99. * @return a string representation of this
  100. * <code>PrivilegedActionException</code>
  101. */
  102. public String toString() {
  103. return getClass().getName() + " <<" + this.exception.toString() + ">>";
  104. }
  105. }