Câu hỏi phỏng vấn Java
Câu hỏi

What’s the difference between a ClassNotFoundException and NoClassDefFoundError ?

Câu trả lời

The ClassNotFoundException and NoClassDefFoundError in Java are related to the absence of a class at runtime, but they occur in different scenarios and have distinct characteristics.

ClassNotFoundException

  • It is a checked exception, which means it must be declared in a method or constructor's throws clause if it can be thrown by the execution of the method or constructor and propagated upwards[1][2][5][6].
  • ClassNotFoundException occurs when an application tries to load a class at runtime using methods like Class.forName(), ClassLoader.loadClass(), or ClassLoader.findSystemClass(), and the class with the specified name cannot be found in the classpath[1][2][5][6].
  • This exception is often encountered when the classpath is not correctly updated with the required JAR files, or when making reflective calls to classes that do not exist[1][2][5][6].
  • An example of when this exception might occur is when trying to connect to a database without the database driver in the classpath[2][5].

NoClassDefFoundError

  • It is an error, not an exception, and is considered an unchecked problem[1][2][4][5][6].
  • NoClassDefFoundError occurs when the Java Runtime System does not find the definition of a class that was present at compile time but is missing at runtime[1][2][3][4][5][6].
  • This error typically happens when there is a class f...
middle

middle

Gợi ý câu hỏi phỏng vấn

senior

What is the difference between Applets loaded over the internet and Applets loaded via the file system?

junior

Explain Serialization and Deserialization.

middle

Can == be used on enum in Java ?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào