1. /*
  2. * @(#)ClassNotFoundException.java 1.18 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.lang;
  8. /**
  9. * Thrown when an application tries to load in a class through its
  10. * string name using:
  11. * <ul>
  12. * <li>The <code>forName</code> method in class <code>Class</code>.
  13. * <li>The <code>findSystemClass</code> method in class
  14. * <code>ClassLoader</code> .
  15. * <li>The <code>loadClass</code> method in class <code>ClassLoader</code>.
  16. * </ul>
  17. * <p>
  18. * but no definition for the class with the specified name could be found.
  19. *
  20. * <p>As of release 1.4, this exception has been retrofitted to conform to
  21. * the general purpose exception-chaining mechanism. The "optional exception
  22. * that was raised while loading the class" that may be provided at
  23. * construction time and accessed via the {@link #getException()} method is
  24. * now known as the <i>cause</i>, and may be accessed via the {@link
  25. * Throwable#getCause()} method, as well as the aforementioned "legacy method."
  26. *
  27. * @author unascribed
  28. * @version 1.18, 01/23/03
  29. * @see java.lang.Class#forName(java.lang.String)
  30. * @see java.lang.ClassLoader#findSystemClass(java.lang.String)
  31. * @see java.lang.ClassLoader#loadClass(java.lang.String, boolean)
  32. * @since JDK1.0
  33. */
  34. public class ClassNotFoundException extends Exception {
  35. /**
  36. * use serialVersionUID from JDK 1.1.X for interoperability
  37. */
  38. private static final long serialVersionUID = 9176873029745254542L;
  39. /**
  40. * This field holds the exception ex if the
  41. * ClassNotFoundException(String s, Throwable ex) constructor was
  42. * used to instantiate the object
  43. * @serial
  44. * @since 1.2
  45. */
  46. private Throwable ex;
  47. /**
  48. * Constructs a <code>ClassNotFoundException</code> with no detail message.
  49. */
  50. public ClassNotFoundException() {
  51. super((Throwable)null); // Disallow initCause
  52. }
  53. /**
  54. * Constructs a <code>ClassNotFoundException</code> with the
  55. * specified detail message.
  56. *
  57. * @param s the detail message.
  58. */
  59. public ClassNotFoundException(String s) {
  60. super(s, null); // Disallow initCause
  61. }
  62. /**
  63. * Constructs a <code>ClassNotFoundException</code> with the
  64. * specified detail message and optional exception that was
  65. * raised while loading the class.
  66. *
  67. * @param s the detail message
  68. * @param ex the exception that was raised while loading the class
  69. * @since 1.2
  70. */
  71. public ClassNotFoundException(String s, Throwable ex) {
  72. super(s, null); // Disallow initCause
  73. this.ex = ex;
  74. }
  75. /**
  76. * Returns the exception that was raised if an error occurred while
  77. * attempting to load the class. Otherwise, returns <tt>null</tt>.
  78. *
  79. * <p>This method predates the general-purpose exception chaining facility.
  80. * The {@link Throwable#getCause()} method is now the preferred means of
  81. * obtaining this information.
  82. *
  83. * @return the <code>Exception</code> that was raised while loading a class
  84. * @since 1.2
  85. */
  86. public Throwable getException() {
  87. return ex;
  88. }
  89. /**
  90. * Returns the the cause of this exception (the exception that was raised
  91. * if an error occurred while attempting to load the class; otherwise
  92. * <tt>null</tt>).
  93. *
  94. * @return the cause of this exception.
  95. * @since 1.4
  96. */
  97. public Throwable getCause() {
  98. return ex;
  99. }
  100. }