While learning exceptions i tried the following steps and found a very strange behavior. Please find the steps below.
- Created a Public Class
A
- Create a class
B
- Compiled
A
andB
- Deleted the class
B
before runningA
After doing above steps program runs absolutely fine and throws NoClassDefFoundError
,Now strange thing is when i declare the reference variable inside the try
block and run the program again it throws an uncaught NoClassDefFoundError
public class A{
public static void main(String[] args) {
B m ;//Caught is getting printed if declare above try-catch()
try {
B m = new B();//Here Uncaught exception is throws
} catch (java.lang.NoClassDefFoundError ex) {
System.out.println("Caught!");
}
}
}
class B{
}
I really don't have any idea why this is happening please help me understand this