浅谈十个常见的Java异常出现原因

2025-05-29 0 26

异常是 java 程序中经常遇到的问题,我想每一个 java 程序员都讨厌异常,一 个异常就是一个 bug,就要花很多时间来定位异常问题。

1、nullpointerexception

空指针异常,操作一个 null 对象的方法或属性时会抛出这个异常。具体看上篇文章:空指针常见案例。

2、outofoutofmemoryerror

内存出现异常的一种异常,这不是程序能控制的,是指要分配的对象的内存超出了当前最大的堆内存,需要调整堆内存大小(-xmx)以及优化程序。

3、ioexception

io,即:input, output,我们在读写磁盘文件、网络内容的时候经常会生的一种异常,这种异常是受检查异常,需要进行手工捕获。

如文件读写会抛出 ioexception:

?

1

2
public int read() throws ioexception

public void write(int b) throws ioexception

4、filenotfoundexception

文件找不到异常,如果文件不存在就会抛出这种异常

如定义输入输出文件流,文件不存在会报错:

?

1

2
public fileinputstream(file file) throws filenotfoundexception

public fileoutputstream(file file) throws filenotfoundexception

filenotfoundexception 其实是 ioexception 的子类,同样是受检查异常,需要进行手工捕获。

5、classnotfoundexception

类找不到异常,java开发中经常遇到,是不是很绝望?这是在加载类的时候抛出来的,即在类路径下不能加载指定的类。

看一个示例:

?

1

2

3

4

5

6

7

8
public static <t> class<t> getexistingclass(classloader classloader, string classname) {

try {

return (class<t>) class.forname(classname, true, classloader);

}

catch (classnotfoundexception e) {

return null;

}

}

它是受检查异常,需要进行手工捕获。

6、classcastexception

类转换异常,将一个不是该类的实例转换成这个类就会抛出这个异常

如将一个数字强制转换成字符串就会报这个异常

?

1

2
object x = new integer(0);

system.out.println((string)x);

这是运行时异常,不需要手工捕获。

7、nosuchmethodexception

没有这个方法异常,一般发生在反射调用方法的时候,如:

?

1

2

3

4

5

6

7

8
public method getmethod(string name, class<?>... parametertypes) throws nosuchmethodexception, securityexception {

checkmemberaccess(member.public, reflection.getcallerclass(), true);

method method = getmethod0(name, parametertypes, true);

if (method == null) {

throw new nosuchmethodexception(getname() + "." + name + argumenttypestostring(parametertypes));

}

return method;

}

它是受检查异常,需要进行手工捕获。

8、indexoutofboundsexception

索引越界异常,当操作一个字符串或者数组的时候经常遇到的异常

例:一个arraylist数组中没有元素,而你想获取第一个元素,运行是就会报此类型的错误。

?

1

2

3

4

5

6
public class test{

public static void main(args[] ){

list<string> list = new arraylist<>();

system.out.println(list.get(0));

  }

}

它是运行时异常,不需要手工捕获。

9、arithmeticexception

算术异常,发生在数字的算术运算时的异常,如一个数字除以 0 就会报这个错。

?

1
double n = 3 / 0;

这个异常虽然是运行时异常,可以手工捕获抛出自定义的异常,如:

?

1

2

3

4

5

6

7

8

9
public static timestamp from(instant instant) {

try {

timestamp stamp = new timestamp(instant.getepochsecond() * millis_per_second);

stamp.nanos = instant.getnano();

return stamp;

} catch (arithmeticexception ex) {

throw new illegalargumentexception(ex);

}

}

10、sqlexception

sql异常,发生在操作数据库时的异常

如下面的获取连接:

?

1

2

3

4

5

6

7
public connection getconnection() throws sqlexception {

if (getuser() == null) {

return drivermanager.getconnection(url);

} else {

return drivermanager.getconnection(url, getuser(), getpassword());

}

}

又或者是获取下一条记录的时候:

?

1
1 boolean next() throws sqlexception;

它是受检查异常,需要进行手工捕获。

以上所述是小编给大家介绍的十个常见的java异常出现原因详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对快网idc网站的支持!

原文链接:https://www.cnblogs.com/aiitzzx0116/p/10505476.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 浅谈十个常见的Java异常出现原因 https://www.kuaiidc.com/109437.html

相关文章

发表评论
暂无评论