Diff
between NoClassDefFoundError and ClassNotFoundException
NCDFError
comes when Java runtime fails to find a class at runtime, even though
it was present at compile time. This could happen if some class was
compiled but while creating a jar/war, it got left out. So the error
came when Java tried to load that class during runtime.
CNFException
comes when a class is attempted to be loaded by its name given as a
string to methods like
Class.forName(),
ClassLoader.findSystemClass(),
ClassLoader.loadClass()
So
the NCDFError is non-recoverable as it interferes directly with Java
virtual machine and one cannot be caught with try-catch. But the
CNFException is a recoverable problem as its an exception and can be
caught with try-catch.
So
to sum up, severity of CNFException is lower than that of NCDFError
and it is recoverable by using try-catch.
Fix
for both the above is mostly the same: lookup your included jars and
classes to see which one is missing at runtime.
Diff
between Checked and Unchecked Exception
Checked
Exceptions (CE)
- Exceptions that capture errors caused by wrong input to
programs/methods. These exceptions are required to be specified in
the throws clause of a function and if they are specified, the
calling function must handle them appropriately using throws itself
or by using try-catch. Examples:
Unchecked
Exceptions (UCE)
- These are the exceptions which arise due to faulty logic of the
program. These are not required to be specified by throws clause and
if specified, the calling function is not required to handle them.
Examples :
UCEs
can happen anytime and in any part of the code. While the scope of
CEs is somewhat limited. That’s why UCEs are not required to be
caught explicitly.
Java
compilers enforce handling of CEs at runtime but do not enforce
handling of UCEs.
All
UCEs are subclasses of RuntimeException whereas
All
CEs are subclasses of Exception.
Interestingly,
RuntimeException itself is subclass of Exception
Got a thought to share or found a bug in the code? We'd love to hear from you: