Joke Collection Website - Blessing messages - The difference between ClassNotFoundException and NoClassDefFoundError

The difference between ClassNotFoundException and NoClassDefFoundError

ClassNotFoundException and NoClassDefFoundError, both of which seem to be exceptions caused by the inability to find the class, but one is an Exceptioin exception and the other is an Error error. By comparing them, we can understand the difference between Error and Exception. .

ClassNotFoundException is an Exception that is generally encountered during reflection. It is an error caused by dynamic loading. Dynamic loading is a business logic deliberately designed by developers, and it has the possibility of failure, so it is recommended to catch it.

NoClassDefFoundError is an Error. When this error occurs, there is no problem during compilation. However, at runtime, when the JVM or ClassLoader loads a certain class and finds that the class cannot be found, this error will be reported. . This is generally a problem with the operating environment, such as a lack of library files or something. This error has nothing to do with business logic and is an error that must be resolved. Otherwise, the software cannot continue to run, so it is not recommended to capture it.