Make delicious recipes!

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

  1. Class.forName(),

  2. ClassLoader.findSystemClass(),

  3. 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:

AclNotFoundException, ActivationException, AlreadyBoundException, ApplicationException, AWTException, BackingStoreException, BadLocationException, CertificateException, ClassNotFoundException, CloneNotSupportedException, DataFormatException, DestroyFailedException, ExpandVetoException, FontFormatException, GeneralSecurityException, GSSException, IllegalAccessException, InstantiationException, InterruptedException, IntrospectionException, InvalidMidiDataException, InvalidPreferencesFormatException, InvocationTargetException, IOException, LastOwnerException, LineUnavailableException, MidiUnavailableException, MimeTypeParseException, NamingException, NoninvertibleTransformException, NoSuchFieldException, NoSuchMethodException, NotBoundException, NotOwnerException, ParseException, ParserConfigurationException, PrinterException, PrintException, PrivilegedActionException, PropertyVetoException, RefreshFailedException, RemarshalException, RuntimeException, SAXException, ServerNotActiveException, SQLException, TooManyListenersException, TransformerException, UnsupportedAudioFileException, UnsupportedCallbackException, UnsupportedFlavorException, UnsupportedLookAndFeelException, URISyntaxException, UserException, XAException



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 :

ArithmeticException, ArrayStoreException, BufferOverflowException, BufferUnderflowException, CannotRedoException, CannotUndoException, ClassCastException, CMMException, ConcurrentModificationException, DOMException, EmptyStackException, IllegalArgumentException, IllegalMonitorStateException, IllegalPathStateException, IllegalStateException, ImagingOpException, IndexOutOfBoundsException, MissingResourceException, NegativeArraySizeException, NoSuchElementException, NullPointerException, ProfileDataException, ProviderException, RasterFormatException, SecurityException, SystemException, UndeclaredThrowableException, UnmodifiableSetException, UnsupportedOperationException


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




Like us on Facebook to remain in touch
with the latest in technology and tutorials!


Got a thought to share or found a
bug in the code?
We'd love to hear from you:

Name:
Email: (Your email is not shared with anybody)
Comment:

Facebook comments:

Site Owner: Sachin Goyal