Java 里面的异常,java 语言程序设计教程 pdf
import java.io.IOException; /** * @Description: FileNotFoundException * @Author: zhangxy * @Date: Created in 2019/11/20 * @Modified By: */ public class ExceptionTest1 { public static void main(String[] args) { FileReader fileReader = null; try { // 实例化 File 对象 File file = new File("test.txt"); // 实例化 FileReader 流,用于数据的读入 fileReader = new FileReader(file); // 读入数据 char[] chars = new char[5]; int length; while ((length = fileReader.read(chars)) != -1) { String s = new String(chars, 0, length); System.out.println(s); } } catch (IOException e) { e.printStackTrace(); } finally { // 不再使用的时候,关闭流资源 if (fileReader != null) { try { fileReader.close(); } catch (IOException e) { e.printStackTrace(); } } } } } 测试结果: java.io.FileNotFoundException: test.txt (系统找不到指定的文件。) at java.io.FileInputStream.open0(Native Method) at java.io.FileInputStream.open(FileInputStream.java:195) at java.io.FileInputStream.<init>(FileInputStream.java:138) at java.io.FileReader.<init>(FileReader.java:72) at com.zxy.java.exception.ExceptionTest1.main(ExceptionTest1.java:21) []( )三、运行时异常 -------------------------------------------------------------------------- _**运行时异常也被称作非受检异常**_ > 是指编译器不要求必须处理的异常。一般是指编程时的逻辑错误。 #### []( )1.空指针异常 > 当程序运行时,对象未初始化或为空时,将会抛出空指针异常。 package com.zxy.java.exception; /** * @Description: NullPointerException * @Author: zhangxy * @Date: Created in 2019/11/20 * @Modified By: */ public class ExceptionTest2 { public static void main(String[] args) { String s = "HelloWorld"; s = null; System.out.println(s.charAt(1)); } } 测试结果: Exception in thread "main" java.lang.NullPointerException at com.zxy.java.exception.ExceptionTest2.main(ExceptionTest2.java:13) #### []( )2\. 数组下标越界异常 > 指使用非法索引访问数组。索引为负值或大于或等于数组的大小。将会抛出数组下标越界异常。 package com.zxy.java.exception; /** * @Description: ArrayIndexOutOfBoundsException * @Author: zhangxy * @Date: Created in 2019/11/20 * @Modified By: */ public class ExceptionTest3 { public static void main(String[] args) { int[] array = new int[5]; System.out.println(array[6]); } } 测试结果: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6 at com.zxy.java.exception.ExceptionTest3.main(ExceptionTest3.java:12) #### []( )3\. 数字格式异常 > 一般由 Integer.valueOf(String param) 或者 Integer.parseInt(String param) 引起数字异常 package com.zxy.java.exception; /** * @Description: NumberFormatException * @Author: zhangxy * @Date: Created in 2019/11/20 * @Modified By: */ public class ExceptionTest4 { public static void main(String[] args) { String str = "111"; str = "abc"; int num = Integer.parseInt(str); System.out.println(num); } } Exception in thread "main" java.lang.NumberFormatException: For input string: "abc" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:580) at java.lang.Integer.parseInt(Integer.java:6 ``` 《Android 学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》 浏览器打开:qq.cn.hn/FTe 免费领取 ``` 15) at com.zxy.java.exception.ExceptionTest4.main(ExceptionTest4.java:13) #### []( )4\. 类型转换异常 > 当程序试图将对象强制转换为不是该实例的子类时,将会抛出类型转换异常 package com.zxy.java.exception; import java.util.Date; /** * @Description: ClassCastException * @Author: zhangxy * @Date: Created in 2019/11/20 * @Modified By: */ public class ExceptionTest5 { public static void main(String[] args) { Object obj = new Date(); String str = (String) obj; } } Exception in thread "main" java.lang.ClassCastException: java.util.Date cannot be cast to java.lang.String at com.zxy.java.exception.ExceptionTest5.main(ExceptionTest5.java:14) #### []( )5\. 算术异常 > 当程序中出现异常的运算条件时,将抛出算术异常。例如,一个整数“除以零”时,抛出算术异常。 package com.zxy.java.exception; import java.util.Date; /** * @Description: ArithmeticException * @Author: zhangxy * @Date: Created in 2019/11/20 * @Modified By: */ public class ExceptionTest6 { public static void main(String[] args) { int m = 8; int n = 0; System.out.println(m / n); }
评论