ClassNotFoundException:
ClassNotFoundException is a runtime exception that is thrown when an application
triesto load a class at runtime using the:
Class.forName() or loadClass() or findSystemClass() methods ,
and the class with specified name are not found in the classpath.
For example, the below program will throw ClassNotFoundException
if the mentioned class “oracle.jdbc.driver.OracleDriver” is not found
in the classpath.
public class MainClass
{
main
{
try
{
Class
ClassNotFoundException
{
e
}
}
}
o/p:-
java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDrive
NoClassDefFoundError:
NoClassDefFoundError is an error that is thrown when the Java Runtime System tries to load the definition of a class, and that class definition is no longer available. The required class definition was present at compile time, but it was missing at runtime.
class A
{
// some code
}
public class B
{
main
{
A
}
}
When you compile the above program, two .class files will be generated.
One is A.class and another one is B.class.
If you remove the A.class file and run the B.class file, Java Runtime System will throw NoClassDefFoundError like below:
o/p:-
Exception in thread "main" java.lang.NoClassDefFoundError: A
at MainClass.main(MainClass.java:10)
Caused by: java.lang.ClassNotFoundException: A
printStackTrace():-
It's a method on
Exception
instances that prints the stack trace of the instance to System.err
.
It's a very simple, but very useful tool for diagnosing an exceptions. It tells you what happened and where in the code this happened.
Here's an example of how it might be used in practice:
try {
// ...
} catch (SomeException e) {
e.printStackTrace();
}
No comments:
Post a Comment