6 Java Exceptions that Haunts a Newbie Java Developer(Java菜鸟6种常见的异常)

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.

  1. 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.
  2. 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
  3. 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. 

  4. 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.
  5. 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.
  6. 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

时间: 2024-10-03 20:59:58

6 Java Exceptions that Haunts a Newbie Java Developer(Java菜鸟6种常见的异常)的相关文章

Java中常见的异常类型

异常大体分为编译异常和运行异常两类,如果用软件开发(如Eclipse)编译异常在写代码时得到提醒,而运行异常需要在运行时才能得到提示. 下面介绍几种常见的异常类型,便于在出错时对程序进行查错: (1)Exception    各种异常的根类 如果你不知道将发生的异常集体属于哪,而可以肯定种这个地方会出现异常时,可以把异常类型定义为Exception. (2)ArithmeticException     算数运算错误异常 这个异常是在算数运算时经常遇到的,常见的情况是除数为0时抛出的异常. (3

Top 4 Java exceptions Raygun can help fix

Top 4 Java exceptions Raygun can help fix Provider RelatedResourceTech Stuff Callum Gavin April 20, 2015 Leave a comment There are a bunch of Java exceptions that are common to all code bases, and frequently occur at both compile time when implementi

【Java学习笔记之三十三】详解Java中try,catch,finally的用法及分析

这一篇我们将会介绍java中try,catch,finally的用法 以下先给出try,catch用法: try { //需要被检测的异常代码 } catch(Exception e) { //异常处理,即处理异常代码 } 代码区如果有错误,就会返回所写异常的处理. 首先要清楚,如果没有try的话,出现异常会导致程序崩溃.而try则可以保证程序的正常运行下去,比如说: try { int i = 1/0; } catch(Exception e) { ........ } 一个计算的话,如果除数

Java链接Redis时出现 “ERR Client sent AUTH, but no password is set” 异常的原因及解决办法

[错误提示] redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the poolat redis.clients.util.Pool.getResource(Pool.java:53)at redis.clients.jedis.JedisPool.getResource(JedisPool.java:226)at cn.hncu.RedisPool.getJedis(Re

Java学习之路(一)了解Java

Java“白皮书”的关键术语 1)简单性 相对于C++:没有头文件.指针运算.结构.联合.操作符重载.虚基类. 另一方面是小:java微型版(Java Micro Edition)用于嵌入式设备 2)面向对象 与C++比没有多继承,Java的继承具有单根性,取而代之的是接口的概念,以及java的元类(metaclass)模型 3)网络技术 java有一个扩展的例程库,用于处理像HTTP和FTP之类的TCP/IP协议. 4)健壮性 java的指针模型可以消除重写内存和损坏数据的可能性. 5)安全性

java几种常见的排序算法总结

[java] view plain copy /*************几种常见的排序算法总结***************************/ package paixu; public class PaiXu { final int MAX=20; int num[]=new int[MAX]; { System.out.print("生成的随机数组是:"); for(int i=0;i<20;i++){ num[i]=(int)(Math.random()*100)

java环境安装、配置环境变量、java工具介绍

1.java的安装 1.在官网下载JDK安装包,链接如下: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 2.安装JDK,按照默认的方式安装JDK,只需一直点击下一步即可,此步省略 3.JDK安装成功之后,配置系统环境变量,通过右键点击计算机以此点击属性.高级系统设置.配置环境变量如下图所示: 4.点击环境变量之后会弹出一下窗口,点击新建 5.点击新建窗口之后分别输入JAVA

转!!Java中关于Null的9个解释(Java Null详解)

对于Java程序员来说,null是令人头痛的东西.时常会受到空指针异常(NPE)的骚扰.连Java的发明者都承认这是他的一项巨大失误.Java为什么要保留null呢?null出现有一段时间了,并且我认为Java发明者知道null与它解决的问题相比带来了更多的麻烦,但是null仍然陪伴着Java. 我越发感到惊奇,因为java的设计原理是为了简化事情,那就是为什么没有浪费时间在指针.操作符重载.多继承实现的原因,null却与此正好相反.好吧,我真的不知道这个问题的答案,我知道的是不管null被Ja

疯狂Java学习笔记(59)-----------50道Java线程面试题

50道Java线程面试题 下面是Java线程相关的热门面试题,你可以用它来好好准备面试. 1) 什么是线程? 线程是操作系统能够进行运算调度的最小单位,它被包含在进程之中,是进程中的实际运作单位.程序员可以通过它进行多处理器编程,你可以使用多线程对运算密集型任务提速.比如,如果一个线程完成一个任务要100毫秒,那么用十个线程完成改任务只需10毫秒.Java在语言层面对多线程提供了卓越的支持,它也是一个很好的卖点.欲了解更多详细信息请点击这里. 2) 线程和进程有什么区别? 线程是进程的子集,一个