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