1. /*
  2. * @(#)ClassNotFoundException.java 1.9 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.lang;
  8. import java.io.PrintStream;
  9. import java.io.PrintWriter;
  10. /**
  11. * Thrown when an application tries to load in a class through its
  12. * string name using:
  13. * <ul>
  14. * <li>The <code>forName</code> method in class <code>Class</code>.
  15. * <li>The <code>findSystemClass</code> method in class
  16. * <code>ClassLoader</code> .
  17. * <li>The <code>loadClass</code> method in class <code>ClassLoader</code>.
  18. * </ul>
  19. * <p>
  20. * but no definition for the class with the specifed name could be found.
  21. *
  22. * @author unascribed
  23. * @version 1.9, 11/29/01
  24. * @see java.lang.Class#forName(java.lang.String)
  25. * @see java.lang.ClassLoader#findSystemClass(java.lang.String)
  26. * @see java.lang.ClassLoader#loadClass(java.lang.String, boolean)
  27. * @since JDK1.0
  28. */
  29. public
  30. class ClassNotFoundException extends Exception {
  31. /**
  32. * use serialVersionUID from JDK 1.1.X for interoperability
  33. */
  34. private static final long serialVersionUID = 9176873029745254542L;
  35. /**
  36. * This field holds the exception ex if the
  37. * ClassNotFoundException(String s, Throwable ex) constructor was
  38. * used to instantiate the object
  39. * @serial
  40. * @since JDK 1.2
  41. */
  42. private Throwable ex;
  43. /**
  44. * Constructs a <code>ClassNotFoundException</code> with no detail message.
  45. */
  46. public ClassNotFoundException() {
  47. super();
  48. }
  49. /**
  50. * Constructs a <code>ClassNotFoundException</code> with the
  51. * specified detail message.
  52. *
  53. * @param s the detail message.
  54. */
  55. public ClassNotFoundException(String s) {
  56. super(s);
  57. }
  58. /**
  59. * Constructs a <code>ClassNotFoundException</code> with the
  60. * specified detail message and optional exception that was
  61. * raised while loading the class.
  62. *
  63. * @param s the detail message
  64. * @param ex the exception that was raised while loading the class
  65. * @since JDK1.2
  66. */
  67. public ClassNotFoundException(String s, Throwable ex) {
  68. super(s);
  69. this.ex = ex;
  70. }
  71. /**
  72. * Returns the exception that was raised if an error occurred while
  73. * attempting to load the class. Otherwise, returns null.
  74. *
  75. * @since JDK1.2
  76. */
  77. public Throwable getException() {
  78. return ex;
  79. }
  80. /**
  81. * Prints the stack backtrace.
  82. *
  83. * If an exception occurred during class loading it prints that
  84. * exception's stack trace, or else prints the stack backtrace of
  85. * this exception.
  86. *
  87. * @see java.lang.System#err
  88. */
  89. public void printStackTrace() {
  90. printStackTrace(System.err);
  91. }
  92. /**
  93. * Prints the stack backtrace to the specified print stream.
  94. *
  95. * If an exception occurred during class loading it prints that
  96. * exception's stack trace, or else prints the stack backtrace of
  97. * this exception.
  98. */
  99. public void printStackTrace(PrintStream ps) {
  100. synchronized (ps) {
  101. if (ex != null) {
  102. ps.print("java.lang.ClassNotFoundException: ");
  103. ex.printStackTrace(ps);
  104. } else {
  105. super.printStackTrace(ps);
  106. }
  107. }
  108. }
  109. /**
  110. * Prints the stack backtrace to the specified print writer.
  111. *
  112. * If an exception occurred during class loading it prints that
  113. * exception's stack trace, or else prints the stack backtrace of
  114. * this exception.
  115. */
  116. public void printStackTrace(PrintWriter pw) {
  117. synchronized (pw) {
  118. if (ex != null) {
  119. pw.print("java.lang.ClassNotFoundException: ");
  120. ex.printStackTrace(pw);
  121. } else {
  122. super.printStackTrace(pw);
  123. }
  124. }
  125. }
  126. }