5.1 异常处理的方式
5.2 异常 throw
方法的调用者要么进行异常捕获处理,要么使用 throws 声明处理,当然也可以通过 throw 关键字继续抛出异常。
语法:
thorw new 异常类名(实参);
示例:
package com.github.demo2;
/**
*
* @author maxiaoke.com
* @version 1.0
* @return
*/
public class TestException {
public static void main(String[] args) {
int[] arr = {1, 2, 3};
int element = getElement(arr, 2);
System.out.println("element = " + element);
/**
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 数组索引越界
at com.github.demo2.TestException.getElement(TestException.java:27)
at com.github.demo2.TestException.main(TestException.java:16)
*/
int e = getElement(arr, 3);
System.out.println("e = " + e);
System.out.println("main方法结束");
}
public static int getElement(int[] arr, int index) {
if (null == arr) {
throw new NullPointerException("要访问的arr数组不存在");
}
if (index < 0 || index > arr.length - 1) {
throw new ArrayIndexOutOfBoundsException("数组索引越界");
}
return arr[index];
}
}
5.3 声明异常 throws
声明异常 :将异常标识出来,告诉调用者。如果方法内通过 throw 关键字 编译时异常(非runtime异常) ,而没有捕获处理,那么必须通过throws 进行声明,让调用者进行处理。
关键字 throws 用在方法声明上,告诉调用者当前方法不处理异常,而是提醒该方法的调用者来处理异常。
语法:
修饰符 返回值类型 方法名(形参列表) throws 异常类名1,异常类名2...{}
示例:
package com.github.demo3;
import java.io.File;
import java.io.FileNotFoundException;
/**
* @author maxiaoke.com
* @version 1.0
* @return
*/
public class TestException {
public static void main(String[] args) throws FileNotFoundException {
readFile("java秘籍");
}
public static void readFile(String filePath) throws FileNotFoundException {
File file = new File(filePath);
if (!file.exists()) {
throw new FileNotFoundException(filePath + "文件不存在");
}
}
}
5.4 try…catch…finally
如果一个方法内没有抛出异常,该异常对象会被抛出给调用者方法中处理。如果异常没有在调用者方法中处理,它会继续抛给这个调用方法的上层方法。这个过程将一直继续下去,直到异常被处理,这个过程称为 捕获异常 。
如果一个异常回到 main 方法处,并且 main 方法也不处理,则程序终止运行。
语法:
try{
...... //可能产生异常的代码 }
catch( ExceptionName1 e ){
...... //当产生ExceptionName1型异常时的处置措施 }
catch( ExceptionName2 e ){
...... //当产生ExceptionName2型异常时的处置措施 }
finally{
...... //无论是否发生异常,都无条件执行的语句
}
解释:
示例:
package com.github.demo4;
import java.io.File;
import java.io.FileNotFoundException;
/**
* @author maxiaoke.com
* @version 1.0
* @return
*/
public class TestException {
public static void main(String[] args) {
readFile("不敲代码学会Java秘籍.txt");
System.out.println("继续学习吧...");
}
public static void readFile(String filePath) {
File file = new File(filePath);
try {
if (!file.exists()) {
throw new FileNotFoundException(filePath + "文件不存在");
}
if (!file.isFile()) {
throw new IllegalAccessException(filePath + "非法访问");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("这里的代码一定会执行");
}
}
}
5.5 不捕获异常的情况
① 如果抛出的异常都是 Exception 或其子类型的运行时异常,这些运行时异常的特点是:即使没有使用 try 和 catch 捕获,Java 自己也能捕获,并且编译通过,但是运行的时候发生异常会使得程序终止运行。
② 如果抛出的异常都是 IOException 等类型的非运行异常,则 必须捕获,否则编译错误 。换言之,我们必须处理编译时异常,将这类类型的异常捕获,转换为运行时异常。
5.6 try … catch 新特性(JDK 1.7)
如果多个 catch 分支的异常处理代码一致的,在 JDK 1.7 之前是非常繁琐的。
try {
}catch (异常类型 e){
// 处理异常的代码
}catch (异常类型 e){
// 处理异常的代码
}
但是,在 JDK 1.7 的时候可以简化上述的代码。
try {
}catch (异常类型1 | 异常类型2 e){
// 处理异常的代码
}
示例:
package com.github.day09;
import java.util.Scanner;
/**
* @author maxiaoke.com
* @version 1.0
*/
public class TryCatchJDK7 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
try {
System.out.print("请输入第一个整数:");
int a = input.nextInt();
System.out.print("请输入第二个整数:");
int b = input.nextInt();
int result = a / b;
System.out.println("result = " + result);
} catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
System.out.println("数字格式不正确,请输入两个整数");
} catch (ArithmeticException e) {
System.out.println("第二个整数不能为0");
} catch (Exception e) {
System.out.println("其他错误");
}
input.close();
}
}