1. /*
  2. * @(#)PrivilegedActionException.java 1.16 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.security;
  8. /**
  9. * This exception is thrown by
  10. * <code>doPrivileged(PrivilegedExceptionAction)</code> and
  11. * <code>doPrivileged(PrivilegedExceptionAction,
  12. * AccessControlContext context)</code> to indicate
  13. * that the action being performed threw a checked exception. The exception
  14. * thrown by the action can be obtained by calling the
  15. * <code>getException</code> method. In effect, an
  16. * <code>PrivilegedActionException</code> is a "wrapper"
  17. * for an exception thrown by a privileged action.
  18. *
  19. * <p>As of release 1.4, this exception has been retrofitted to conform to
  20. * the general purpose exception-chaining mechanism. The "exception thrown
  21. * by the privileged computation" that is provided at construction time and
  22. * accessed via the {@link #getException()} method is now known as the
  23. * <i>cause</i>, and may be accessed via the {@link Throwable#getCause()}
  24. * method, as well as the aforementioned "legacy method."
  25. *
  26. * @see PrivilegedExceptionAction
  27. * @see AccessController#doPrivileged(PrivilegedExceptionAction)
  28. * @see AccessController#doPrivileged(PrivilegedExceptionAction,AccessControlContext)
  29. */
  30. public class PrivilegedActionException extends Exception {
  31. // use serialVersionUID from JDK 1.2.2 for interoperability
  32. private static final long serialVersionUID = 4724086851538908602L;
  33. /**
  34. * @serial
  35. */
  36. private Exception exception;
  37. /**
  38. * Constructs a new PrivilegedActionException "wrapping"
  39. * the specific Exception.
  40. *
  41. * @param exception The exception thrown
  42. */
  43. public PrivilegedActionException(Exception exception) {
  44. super((Throwable)null); // Disallow initCause
  45. this.exception = exception;
  46. }
  47. /**
  48. * Returns the exception thrown by the privileged computation that
  49. * resulted in this <code>PrivilegedActionException</code>.
  50. *
  51. * <p>This method predates the general-purpose exception chaining facility.
  52. * The {@link Throwable#getCause()} method is now the preferred means of
  53. * obtaining this information.
  54. *
  55. * @return the exception thrown by the privileged computation that
  56. * resulted in this <code>PrivilegedActionException</code>.
  57. * @see PrivilegedExceptionAction
  58. * @see AccessController#doPrivileged(PrivilegedExceptionAction)
  59. * @see AccessController#doPrivileged(PrivilegedExceptionAction,
  60. * AccessControlContext)
  61. */
  62. public Exception getException() {
  63. return exception;
  64. }
  65. /**
  66. * Returns the the cause of this exception (the exception thrown by
  67. * the privileged computation that resulted in this
  68. * <code>PrivilegedActionException</code>).
  69. *
  70. * @return the cause of this exception.
  71. * @since 1.4
  72. */
  73. public Throwable getCause() {
  74. return exception;
  75. }
  76. public String toString() {
  77. String s = getClass().getName();
  78. return (exception != null) ? (s + ": " + exception.toString()) : s;
  79. }
  80. }