ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

RuntimeException运行时异常

2021-09-21 10:34:31  阅读:139  来源: 互联网

标签:String RuntimeException public str main 异常 class 运行


  派生于RuntimeException的异常,如被 0 除、数组下标越界、空指针等,其产生比较频繁,处理麻烦,如果显式的声明或捕获将会对程序可读性和运行效率影响很大。 因此由系统自动检测并将它们交给缺省的异常处理程序(用户可不必对其处理)。

      这类异常通常是由编程错误导致的,所以在编写程序时,并不要求必须使用异常处理机制来处理这类异常,经常需要通过增加“逻辑处理来避免这些异常”。

【示例】ArithmeticException异常:试图除以0

1 2 3 4 5 6 public class Test3 {     public static void main(String[] args) {         int b=0;         System.out.println(1/b);     } }

    

图6-4 ArithmeticException异常.png

 

      解决如上异常需要修改代码:

1 2 3 4 5 6 7 8 public class Test3 {     public static void main(String[] args) {         int b=0;         if(b!=0){             System.out.println(1/b);         }     } }

      当程序访问一个空对象的成员变量或方法,或者访问一个空数组的成员时会发生空指针异常(NullPointerException)。怎么处理?

【示例】NullPointerException异常

1 2 3 4 5 6 public class Test4 { public static void main(String[] args) { String str=null; System.out.println(str.charAt(0)); } }

 

图6-5 NullPointerException异常.png

 

      解决空指针异常,通常是增加非空判断:

1 2 3 4 5 6 7 8 public class Test4 {     public static void main(String[] args) {         String str=null;         if(str!=null){             System.out.println(str.charAt(0));         }     } }

      在引用数据类型转换时,有可能发生类型转换异常(ClassCastException)。

【示例】ClassCastException异常

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 class Animal{       } class Dog extends Animal{       } class Cat extends Animal{       } public class Test5 {     public static void main(String[] args) {         Animal a=new Dog();         Cat c=(Cat)a;     } }

 

图6-6 ClassCastException异常.png

 

      解决ClassCastException的典型方式:

1 2 3 4 5 6 7 8 public class Test5 {     public static void main(String[] args) {         Animal a = new Dog();         if (a instanceof Cat) {             Cat c = (Cat) a;         }     } }

      当程序访问一个数组的某个元素时,如果这个元素的索引超出了0~数组长度-1这个范围,则会出现数组下标越界异常(ArrayIndexOutOfBoundsException)。

【示例】ArrayIndexOutOfBoundsException异常

1 2 3 4 5 6 public class Test6 {     public static void main(String[] args) {         int[] arr = new int[5];         System.out.println(arr[5]);     } }

 

图6-7 ArrayIndexOutOfBoundsException异常.png

 

      解决数组索引越界异常的方式,增加关于边界的判断:

1 2 3 4 5 6 7 8 9 public class Test6 {     public static void main(String[] args) {         int[] arr = new int[5];         int a = 5;         if (a < arr.length) {             System.out.println(arr[a]);         }     } }

      在使用包装类将字符串转换成基本数据类型时,如果字符串的格式不正确,则会出现数字格式异常(NumberFormatException)。

【示例】NumberFormatException异常

1 2 3 4 5 6 public class Test7 {     public static void main(String[] args) {         String str = "1234abcf";         System.out.println(Integer.parseInt(str));     } }

 

图6-8 NumberFormatException异常.png

 

      数字格式化异常的解决,可以引入正则表达式判断是否为数字:

1 2 3 4 5 6 7 8 9 10 11 12 13 import java.util.regex.Matcher; import java.util.regex.Pattern;   public class Test7 {     public static void main(String[] args) {         String str = "1234abcf";         Pattern p = Pattern.compile("^\\d+$");         Matcher m = p.matcher(str);         if (m.matches()) { // 如果str匹配代表数字的正则表达式,才会转换             System.out.println(Integer.parseInt(str));         }     } }

注意事项

      1. 在方法抛出异常之后,运行时系统将转为寻找合适的异常处理器(exception handler)。潜在的异常处理器是异常发生时依次存留在调用栈中的方法的集合。当异常处理器所能处理的异常类型与方法抛出的异常类型相符时,即为合适的异常处理器。

      2. 运行时系统从发生异常的方法开始,依次回查调用栈中的方法,直至找到含有合适异常处理器的方法并执行。当运行时系统遍历调用栈而未找到合适的异常处理器,则运行时系统终止。同时,意味着Java程序的终止。

标签:String,RuntimeException,public,str,main,异常,class,运行
来源: https://www.cnblogs.com/huaxiansheng/p/15316078.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有