一,throws关键字
异常处理的第一种方式,声明异常,交给别人处理。
作用:当方法内部抛出异常对象的时候,我们必须处理这个异常对象。
可使用throws关键字处理异常对象,会把异常对象抛出给方法的调用者处理(自己不处理,给别人处理),最终交给JVM处理(即中断处理)。
使用格式:在方法声明时使用。
修饰符 返回值类型 方法名(参数列表) throws AaaException,BbbException…{
throw new AaaException(“产生原因”);
throw new BbbException(“产生原因”);
}
注意:
1,throws关键字必须写在方法声明处;
2,throws关键字后边声明的异常必须是Exception或者说Exception的子类;
3,方法内部抛出了多个异常对象,那么throws后边必须声明多个异常(如果多个异常类有继承关系,那么直接声明父类异常即可);
4,调用了一个声明异常的方法,我们就必须处理声明的异常。要么继续使用throws声明抛出,交给方法的调用者处理,最终交给JVM,要么try…catch自己处理。
import java.util.Objects;
public class DemoThrow {
public static void main(String[] args) {
int[] arr = null;
// int[] arr = {};
int res = getElement(arr, 3);
System.out.println(res);
}
private static int getElement(int[] arr, int index){
//判断参数的合法性,检测是否为空,下列两种方法
if(arr == null){
throw new NullPointerException("传入的数组是空数组");
}
// Objects.requireNonNull(arr,"传入的数组是空数组"); //java.lang.NullPointerException: 传入的数组是空数组
if (index < 0 || index >= arr.length){
throw new ArrayIndexOutOfBoundsException("数组越界异常");
}
int num = arr[index];
return num;
}
}
二,try…catch
异常处理的第二种方式,自己处理异常。
格式:try{
可能产生异常的代码;
}catch(异常类名 变量名){
异常处理逻辑
}catch(异常类名 变量名){
异常处理逻辑
}…
注意:
1,try中可能会抛出多个异常对象,那么就可以使用多个catch来处理这些异常对象;
2,如果try中产生异常,会执行catch异常处理逻辑,之后执行后面的代码;如果try中未产生异常,则不会执行catch异常处理逻辑,之后还是会执行后面的代码。
import java.io.IOException;
public class DemoTryCatch {
public static void main(String[] args) {
try {
readFile("c://b.txt");
}catch (IOException e){
System.out.println(e); //java.io.IOException: 文件格式不匹配!
}
System.out.println("执行完毕!"); //都会执行该代码
}
public static void readFile(String fileName) throws IOException {
/* FileNotFoundException entends IOException extends Exception*/
if (!fileName.endsWith(".txt")){
throw new IOException("文件格式不匹配!");
}
System.out.println("文件格式没问题!");
}
}
三,throwable定义了异常处理的方法
1,String getMessage():返回throwable的简短描述;
2,String toString():返回throwable的详细消息字符串;
3,void printStackTrace():JVM打印异常对象,默认此方法,打印的异常信息最全面。
try {
readFile("c://b.tx");
}catch (IOException e){
System.out.println(e); //java.io.IOException: 文件格式不匹配!
System.out.println(e.toString());//java.io.IOException: 文件格式不匹配!
System.out.println(e.getMessage());//文件格式不匹配!
e.printStackTrace();
/*java.io.IOException: 文件格式不匹配!
at day5_06.DemoTryCatch.readFile(DemoTryCatch.java:22)
at day5_06.DemoTryCatch.main(DemoTryCatch.java:8)*/
}finally {
System.out.println("释放资源"); //无论是否有异常,均会执行。 释放资源
}
System.out.println("执行完毕!"); //都会执行该代码
四,多个异常的捕获处理方式
1,多个异常分别处理;
2,多个异常一次捕获,多次处理;
3,多次异常一次捕获,一次处理。
五,其他注意情况
finally中含有return值(避免出现这种情况):
public class DemoFinallyReturn {
public static void main(String[] args) {
int a = getA();
System.out.println(a); //100
}
public static int getA(){
/*finally 中有return值,永远返回finally中的值,要避免产生这种情况*/
int a = 10;
try {
return a;
}catch (Exception e){
System.out.println(a);
}finally {
a = 100;
return a;
}
}
}
子类与父类异常注意事项:
public class DemoFu {
public void show1() throws ClassCastException,NullPointerException{}
public void show2() throws IndexOutOfBoundsException{}
public void show3() throws IndexOutOfBoundsException{}
public void show4(){}
}
class Zi extends DemoFu{
//子类重写父类方法,抛出和父类相同的异常
public void show1() throws ClassCastException,NullPointerException{}
//子类重写父类方法,抛出父类异常的子类
public void show2() throws ArrayIndexOutOfBoundsException{}
//子类重写父类方法,不抛出异常
public void show3(){}
//父类方法没有抛出异常,子类方法也不可抛出异常,若抛出,只能自己捕获处理
public void show4(){
try {
throw new Exception("出现异常");
} catch (Exception e) {
e.printStackTrace();
}
}
}