1. /*
  2. * @(#)PrivilegedActionException.java 1.4 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.security;
  8. import java.io.PrintWriter;
  9. import java.io.PrintStream;
  10. /**
  11. * This exception is thrown by
  12. * <code>doPrivileged(PrivilegedExceptionAction)</code> and
  13. * <code>doPrivileged(PrivilegedExceptionAction,
  14. * AccessControlContext context)</code> to indicate
  15. * that the action being performed threw a checked exception. The exception
  16. * thrown by the action can be obtained by calling the
  17. * <code>getException</code> method. In effect, an
  18. * <code>PrivilegedActionException</code> is a "wrapper"
  19. * for an exception thrown by a privileged action.
  20. *
  21. * @see PrivilegedExceptionAction
  22. * @see AccessController#doPrivileged(PrivilegedExceptionAction)
  23. * @see AccessController#doPrivileged(PrivilegedExceptionAction,AccessControlContext)
  24. */
  25. public
  26. class PrivilegedActionException extends Exception {
  27. /**
  28. * @serial
  29. */
  30. private Exception exception;
  31. /**
  32. * Constructs a new PrivilegedActionException "wrapping"
  33. * the specific Exception
  34. *
  35. * @param exception The exception thrown
  36. */
  37. public PrivilegedActionException(Exception exception) {
  38. this.exception = exception;
  39. }
  40. /**
  41. * Returns the exception thrown by the privileged computation that
  42. * resulted in this <code>PrivilegedActionException</code>.
  43. *
  44. * @return the exception thrown by the privileged computation that
  45. * resulted in this <code>PrivilegedActionException</code>.
  46. * @see PrivilegedExceptionAction
  47. * @see AccessController#doPrivileged(PrivilegedExceptionAction)
  48. * @see AccessController#doPrivileged(PrivilegedExceptionAction,
  49. * AccessControlContext)
  50. */
  51. public Exception getException() {
  52. return exception;
  53. }
  54. /**
  55. * Prints the stack trace of the exception that occurred.
  56. *
  57. * @see java.lang.System#err
  58. */
  59. public void printStackTrace() {
  60. printStackTrace(System.err);
  61. }
  62. /**
  63. * Prints the stack trace of the exception that occurred to the
  64. * specified print stream.
  65. */
  66. public void printStackTrace(PrintStream ps) {
  67. synchronized (ps) {
  68. if (exception != null) {
  69. ps.print("java.security.PrivilegedActionException: ");
  70. exception.printStackTrace(ps);
  71. } else {
  72. super.printStackTrace(ps);
  73. }
  74. }
  75. }
  76. /**
  77. * Prints the stack trace of the exception that occurred to the
  78. * specified print writer.
  79. */
  80. public void printStackTrace(PrintWriter pw) {
  81. synchronized (pw) {
  82. if (exception != null) {
  83. pw.print("java.security.PrivilegedActionException: ");
  84. exception.printStackTrace(pw);
  85. } else {
  86. super.printStackTrace(pw);
  87. }
  88. }
  89. }
  90. }