第十二章:异常处理和文本I/O
一.异常处理概述
1.作用
异常处理使得程序可以处理非预期的情况,并且继续正常的运行
2.在java中,运行时错误会当做异常处理,异常就是一个对象
3.异常是从方法抛出的,方法的调用者捕获以及处理异常
try{
//正常情况下的运行情况
}
catch(type ex){//type:捕获的异常类型 ex:catch块的参数
//处理异常
}
二.异常类型
1.系统错误
2.异常是对象,而对象都采用类来定义,异常的根类是java.long.Throwable
也可以通过继承Exception类或者Exception的子类来创建自己的异常类
3.常见异常
异常 | 说明 |
---|---|
ArrayIndexOutOfBountsException | 数组越界 |
InputMisMatchException | 输入整数时输成浮点数 |
ArthmeticException | 0作为除数(浮点数除以0不会产生异常) |
IllegalArgamentException | 传递给方法的参数非法或不合适 |
注意
下面实例不会抛出异常,但是会溢出
long l=Long.MAX_VALUE+1;//-9223372036854775808
4.声明和捕获异常
声明关键字:throws
声明多个异常用逗号隔开
抛出关键字:throw
public static void method() throws Exception()
以下是声明捕获处理异常示例
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int x = in.nextInt();
int y = in.nextInt();
try {
System.out.println(devide(x, y));//正常情况下运行
} catch (ArithmeticException e) {//捕获异常
System.out.println(e.toString());//处理异常
}
}
public static int devide(int x, int y) {//此处异常为运行时异常,故不需要显式声明
if (y == 0) {
throw new ArithmeticException("0不能作除数");//抛出ArithmeticException异常
} else {
return x / y;
}
}
//输入与返回结果:
//3
//0
//java.lang.ArithmeticException: 0不能作除数
三.异常处理
1.如果异常没有在父类中声明,那么就不能在子类中对其进行继承来声明异常
2.如果异常没有在当前的方法中捕获,就会传给方法的调用者,这个过程一直重复,直到异常被捕获或传到main方法
3.父类的catch块出现在子类的catch快之前会出现编译错误
try{}
catch(Exception e){}
catch(IOException e){}//错误的顺序
4.JDK7中的多捕获异常
catch(Exception1|Exception2|Exception3|……)
try{
method();//如果此方法抛出异常而没有处理,则不会执行下面的语句
System.out.println("hello");
}
5.在没有异常发生时,try-catch块不会引起额外的系统开销
6.finally子句
无论异常是否会发生,finally子句总是会被执行
即使到达finall子句前有return语句finally子句还是会被执行
7.如果一个异常处理器不能处理一个异常或只是让调用者注意到这个异常,那就重新抛出这个异常
8.创建自定义异常类
Exception包括四个构造方法,以下是其中两个
Exception();//没有消息的异常
Exception(message:String);//有消息的异常
四.File类
File类包含了获得文件/目录的属性,以及对文件改名和删除的方法,不包含读写文件内容的方法。
构建File 实例不会在机器上创建一个文件,不管文件是否存在,都可以创建File实例
File file = new File("c:\\z下载\\files\\test\\Demo.txt");
File file = new File("c:/z下载/files/test/Demo.txt");
//使用/或者\\都可以
File类中的方法就不详细介绍了,网上都可以查到。
五.文件的输入和输出
使用Scanner类从文本中读取数据,用PrintWriter类从文本中写数据
java.io.PrinterWriter 可以创建文件,并写数据
必须使用close()关闭流,否则无法将数据保存至文件中
jdk7中可以使用try-with-source可以防止忘记关闭流
public static void main(String[] args) {
// TODO Auto-generated method stub
File file = new File("c:\\z下载\\files\\test\\notexist.txt");//创建File对象
try (PrintWriter printWriter = new PrintWriter(file)) {
for (int i = 0; i < 10; i++) {
printWriter.print(i);//将数据写入文件
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
为创建Scanne从文件中读取数据,必须使用构造方法new File(fileName)创建一个实例。
File file = new File("c:\\z下载\\files\\test\\notexist.txt");
try (Scanner input = new Scanner(file)) {
while (input.hasNext()) {
System.out.print(input.nextInt());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
从Web上读取数据
File file = new File("c:\\z下载\\files\\test\\notexist2.txt");
try {
URL url = new URL("http://www.baidu.com/index.html");
try (Scanner in = new Scanner(url.openStream()); // 打开流
PrintWriter printWriter = new PrintWriter(file)) {
while (in.hasNext()) {
printWriter.print(in.nextLine());//写入文件
}
}
} catch (MalformedURLException e1) {// MalformedURLException 地址输入错误异常
e1.printStackTrace();
}
注意
- 如果需要重复输入,catch中写in.nextLine()丢弃当前行并换行
- 类负责抛出异常,调用该方法的类负责捕获和处理
自定义异常类步骤
- 继承Exception或Exception 的子类
- 按需调用sper(s);
- 另一个类中声明、抛出刚才定义的异常类
- 测试类中捕获、处理异常类
六.总结
通过本章的学习,我知道了Java异常及处理的基本知识,认识了好多异常和出现异常的原因,还学会了使用File类创建文本对象,懂得了文本的输入和输出,并从网络上获取信息。
加油!第十三章待更……