服务器之家

服务器之家 > 正文

java异常处理详细介绍及实例

时间:2020-09-15 15:17     来源/作者:

Java异常层次结构

java异常处理详细介绍及实例

Exception异常

RuntimeException与非RuntimeException异常的区别:

?
1
2
3
4
5
6
非RuntimeException(检查异常):在程序中必须使用trycatch进行处理,否则程序无法编译。
RuntimeException:可以不使用trycatch进行处理,但是如果有异常产生,则异常将由JVM进行处理。
比如:我们从来没有人去处理过NullPointerException异常,它就是运行时异常,并且这种异常还是最常见的异常之一。
出现运行时异常后,系统会把异常一直往上层抛,一直遇到处理代码。如果没有处理块,到最上层,
如果是多线程就由Thread.run()抛出,如果是单线程就被main()抛出。抛出之后,如果是线程,这个线程也就退出了。
如果是主程序抛出的异常,那么这整个程序也就退出了。

Error类和Exception类的父类都是throwable类,他们的区别是:

?
1
2
3
4
Error类一般是指与虚拟机相关的问题,如系统崩溃,虚拟机错误,内存空间不足,方法调用栈溢等。
对于这类错误的导致的应用程序中断,仅靠程序本身无法恢复和和预防,遇到这样的错误,建议让程序终止。
Exception类表示程序可以处理的异常,可以捕获且可能恢复。遇到这类异常,应该尽可能处理异常,
使程序恢复运行,而不应该随意终止异常。

RuntimeException异常

NullPointException异常

一般报Java.lang.NullPointerException的原因有以下几种:

1. 字符串变量未初始化;
2. 对象没有用具体的类初始化;

NullPointException代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package TestNullPointException;
public class TestNullPointException {
  public static void main (String[] args) {
    String str = null;
    try {
      if (str.equals(null)) {
        System.out.println("true");
      } else {
        System.out.println("false");
      }
    } catch (NullPointException e) {
      e.printStackTrace();
    }
  }
}

输出:

?
1
2
java.lang.NullPointerException
  at TestNullPointException.TestNullPointException.main(TestNullPointException.java:6)

ArrayIndexOutOfBoundsException异常

数组下标越界异常,当引用的索引值超出数组长度时,就会发生此异常。

ArrayIndexOutOfBoundsException 代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
package TestArrayIndexOutOfBoundsException;
public class TestArrayIndexOutOfBoundsException {
  public static void main (String[] args) {
    Integer[] array = new Integer[10];
    try {
      Integer temp = array[10];
    } catch (ArrayIndexOutOfBoundsException e) {
      e.printStackTrace();
    }
  }
}

输出:

?
1
2
java.lang.ArrayIndexOutOfBoundsException: 10
  at TestArrayIndexOutOfBoundsException.TestArrayIndexOutOfBoundsException.main(TestArrayIndexOutOfBoundsException.java:6)

ArithmeticException

ArithmeticException是出现异常的运算条件时,抛出此异常。

ArithmeticException代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
 * ArithmeticException
 */
 
 packet TestArithmeticException;
 public class TestArithmeticException {
  public static void main(String[] args) {
    Integer temp = 1;
    try {
      System.out.println(temp/0);
    } catch (ArithmeticException e) {
      e.printStackTrace();
    }
  }
 }

输出:

?
1
2
java.lang.ArithmeticException: / by zero
  at TestArithmeticException.TestArithmeticException.main(TestArithmeticException.java:6)

ArrayStoreException

当你试图将错误类型的对象存储到一个对象数组时抛出的异常。

ArrayStoreException代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
 * ArrayStoreException
 */
 
 packet TestArrayStoreException;
 public class TestArrayStoreException {
  public static void main(String[] args) {
    Object array = new Integer[10];
    try {
      array[0] = "123";
    } catch (ArrayStoreException e) {
      e.printStackTrace();
    }
  }
 }

输出:

?
1
2
Exception in thread "main" java.lang.ArrayStoreException: java.lang.String
  at TestArrayStoreException.TestArrayStoreException.main(TestArrayStoreException.java:6)

NumberFormatException

继承IllegalArgumentException,字符串转换为数字时出现。

NumberFormatException代码如下:

?
1
2
3
4
5
6
7
8
9
10
/**
 * NumberFormatException
 */
package test;
public class ExceptionTest {
   public static void main(String[] args) {
      String s = "q12";
      Integer i = Integer.parseInt(s);
   }
 }

输出:

?
1
2
3
4
5
Exception in thread "main" java.lang.NumberFormatException: For input string: "q12"
  at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
  at java.lang.Integer.parseInt(Integer.java:580)
  at java.lang.Integer.parseInt(Integer.java:615)
  at test.ExceptionTest.main(ExceptionTest.java:8)

ClassCastException

类型转换错误,通常是进行强制类型转换时候出的错误。

ClassCastException代码如下:

?
1
2
3
4
5
6
7
8
9
10
/**
 * ClassCastException 父类赋值给子类,向下转型
 */
package test;
public class ExceptionTest {
   public static void main(String[] args) {
     Object obj=new Object();
     Integer s=(Integer)obj;
   }
 }

输出:

?
1
2
Exception in thread "main" java.lang.ClassCastException: java.lang.Object cannot be cast to java.lang.Integer
  at test.ExceptionTest.main(ExceptionTest.java:5)

 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

原文链接:http://blog.csdn.net/songfeihu0810232/article/details/68948874

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址 2020-08-12
最新idea2020注册码永久激活(激活到2100年)
最新idea2020注册码永久激活(激活到2100年) 2020-07-29
返回顶部

595
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40