Every now and then, I come across various newbies Java developers who are found to get stuck with some of the following common exceptions where I need to explain them all over again. I do believe that this is same with many other senior Java developers who try and help these rookie developers to deal with following exceptions. Thus, I thought of coming up with this article and use it for myself going forward. Please feel free to comment or add to the below exceptions list.
- NoClassDefFoundError: This is one of those exception, with message such as Exception in thread “main” NoClassDefFoundError, that has been most commonly found to welcome newbie Java developers to the Java programming world. They write the hello world, and go to command prompt and write “java” command to execute, and “BOOM”:-). And, it just takes some time for newbies to figure out on how to resolve this exception and see their “hello world” getting printed.NoClassDefFoundError occurs when Java virtual machine (JVM) tries to access a class a run-time and the class is not found, although, the same class was found during compile time. Most famously, this exception is found to occur (mentioned in red) when one tries to invoke a Java program with “java” command and the classpath is not set properly. Some of the reasons for this exception are following:
- Class is not available in Classpath.
- Many a times, startup script is found to override the classpath environment variable. The way to get over is to check with “set” command on windows and see if the classpath includes the class definition.You may want to check further details on this exception on this Javarevisited blog.
- ClassNotFoundException: ClassNotFoundException is one of the other exception that brings nightmares for newbie Java developer as he is getting started with Java programming. Interestingly, it takes some time for an average Java developer to stop confusing between ClassNotFoundException and NoClassDefFoundError. And, thus, the difference between these two exceptions remains as one of the most commonly asked question in interviews for junior Java developers. ClassNotFoundException occurs when JVM tries to load a particular class and does not find the same in the classpath. One of the common place where newbie Java developer experiences it first is code that connects to database using JDBC libraries. There we try to load the driver using the code such as Class.forName( “JDBCdriver” ). A good read on the ClassNotFoundException can be found here. It is also suggested that one should try and understand the Java Classloaders concept to deal with this exception in efficient manner. You may want to check following page on how to set Java classpath in Win/Unix environment. As per the Java docs, the exception occurs in following different scenarios:
- When an attempt is made to load class using Class.forName method call and the .class file does not exist in the class path. This is the most common scenario out of the three listed here.
- When classloader tries to load a class using loadClass method.
- When classloader tries to load a class using findSystemClass
- NullPointerException: NullPointerException is a simpler one to understand and crack and newbies get over it sooner than above two. At the same time, the exception is fairly easy to identifygiven the line number of code where it occured. This exception primarily occurs when the JVM tries to access null in the case where object is required. This exception has been, most commonly, found to be occuring in the case when JVM tries to invoke a method on an object and the object is found to be null. Other cases as mentioned in the Java doc are some of the following:
- Accessing or modifying the method on object which is null.
- Getting the length of array when it is null.
- Accessing or modifying contained objects of array when it is null.
The most easy and common way to get over this type of exception is to add a NOT-NULL check. However, sooner than later, it becomes a practice of Java developer and you find NOT-NULL check everywhere. Interestingly, it is not considered as good coding practice to put NOT-NULL check everywhere. And, the primary reason why the NOT-NULL check practice is used is that developers tend to pass null object for failure cases. Instead, the good coding practice that should be propagated to programmers is to pass an empty object in case the primary/happy path of the program is not satisfied. This would , in turn, reduce NOT-NULL check. However, adoption of this coding practice is easier said than done.
- ClassCastException: This is another one of the familiar exception that comes across the newbies developer when they try to cast an object to a class of which it is not an instance. Again, this is fairly easy to understand, identify and simple to fix. One of the way used to get over this exception when the object type is not known at the runtime is to use “instanceof” to check if the object is instance of a particular class.
- ArrayIndexOutOfBoundsException: This exception is self-explanatory and occurs when JVM tries to access the element of array with an illegal index such as negative (-1) or greater than or equal to size of the array. This one is fairly easy to understand, identify and fix.The code like following will help avoid this exception:for( index = 0; index < array.length; index++ )
Note that index starts at 0 and goes upto 1 less than the size of the array. - IllegalArgumentException: This exception is the least common one and is fairly easy to understand, identify and fix. It occurs when JVM tries to pass an illegal or inappropriate argument to a method.
6 Java Exceptions that Haunts a Newbie Java Developer(Java菜鸟6种常见的异常),布布扣,bubuko.com