Java 里面的异常,java开发视频直播服务

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.(FileInputStream.java:138)

at java.io.FileReader.(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:615)

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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值