I have the below classes.
I have manually compiled the classes using javac and ran the Driver
class.
Later removed the entity.class
and MyCustomException.class
and ran the app like below.
java Driver test
The below error is complained about MyCustomException
is missing but not about the Entity
class. So, not clear why JRE complaining about MyCustomException
class but not the Entity
class.
Indeed I have removed code throw new MyCustomException();
but I did not encounter error about Entity
class.
Caused by: java.lang.NoClassDefFoundError: com/techdisqus/exception/MyCustomException
Please note that the IF condition will NOT be executed as I am passing command argument as test
Why is it throwing an exception is causing to load the MyCustomException
which would be never executed but the JVM does not load any other regular class unless condition is satisfied, as here Entity
class here. Please check Driver.java
below.
MyCustomException.java
public class MyCustomException extends RuntimeException {
}
Entity.java
public class Entity {
}
Driver.java
public class Driver {
public static void main(String[] args) {
String s = args[0];
if("true".equals(s)){
Entity entity = new Entity(); // This is not loaded, unless s is true
throw new MyCustomException(); // this is loaded even s is NOT true.
}else{
System.out.println("success");
}
}
}
Thanks for help
Entity
, it just obscures things. Then see if there is a difference between the error messages you get when youthrow new MyCustomException()
and simply callnew MyCustomException()
without throwing. – KunkelMyCustomException
.. I think I would edit the question to make it clear.. and accepted answer answers this part.. I realised it has to do with throw not about creating object. – Lanai