转自:头条
作者:java一线架构师
1.异常
1.0 异常的概念
2.throw关键字
public class Main{ public static void main(String[] args) { int []arc=null; getelem(arc,0); } private static int getelem(int arc[],int index) { if(arc==null){ throw new NullPointerException("空指针异常!"); } return arc[index]; } }
public class Main{ public static void main(String[] args) { int []arc=new int[3]; getelem(arc,3); } private static int getelem(int arc[],int index) { if(arc==null){ throw new NullPointerException("空指针异常!"); } else if(index>=arc.length||index<0) { throw new ArrayIndexOutOfBoundsException("下标超出数组的范围!"); } return arc[index]; } }
3.Objects 非空判断
import java.util.Objects; public class Main{ public static void main(String[] args) { int arc[]=null; Objects.requireNonNull(arc,"空指针异常"); } }
4.异常处理的第一种方法 throws关键字
import java.io.FileNotFoundException; import java.io.IOException; import java.lang.module.FindException; public class Main { public static void main(String[] args) throws Exception { //判断路径是否是C:\\.txt Filename("C:\\.tx"); } private static void Filename(String s) throws Exception { //FileNotFoundException extends IOException extends Exception if(!s.equals("C:\\.txt")){ throw new FileNotFoundException("该路径不是C:\\.txt"); } if(!s.endsWith(".txt")) throw new IOException("文件后缀不是.txt"); } }
5.异常处理的第二种方法
try...catch()
import java.lang.invoke.MethodHandles; public class Main{ public static void main(String[] args) { int[] arc=new int[3]; try { int getelem = getelem(arc, 3); }catch(ArrayIndexOutOfBoundsException e) { System.out.println("程序由catch处理"); } System.out.println("后续代码"); //如果抛出throws 程序交给JVM处理 程序遇到异常就会中断 } private static int getelem(int arc[],int index) throws ArrayIndexOutOfBoundsException{ if(index<0||index>=arc.length) { throw new ArrayIndexOutOfBoundsException("下标超出数组的长度范围"); } return arc[index]; } }
打印结果:
程序由catch处理后续代码
6.Throwable类中常用的方法
import java.lang.invoke.MethodHandles; public class Main{ public static void main(String[] args) { int[] arc=new int[3]; try { int getelem = getelem(arc, 3); }catch(ArrayIndexOutOfBoundsException e) { System.out.println(e.getMessage()); System.out.println(e.toString()); System.out.println(e); e.printStackTrace(); } System.out.println("后续代码"); //如果抛出throws 程序交给JVM处理 程序遇到异常就会中断 } private static int getelem(int arc[],int index) throws ArrayIndexOutOfBoundsException{ if(index<0||index>=arc.length) { throw new ArrayIndexOutOfBoundsException("下标超出数组的长度范围"); } return arc[index]; } }
打印结果:
下标超出数组的长度范围java.lang.ArrayIndexOutOfBoundsException: 下标超出数组的长度范围java.lang.ArrayIndexOutOfBoundsException: 下标超出数组的长度范围后续代码java.lang.ArrayIndexOutOfBoundsException: 下标超出数组的长度范围at Main.getelem(Main.java:24)at Main.main(Main.java:7)
7.异常处理的注意事项
(1)多个异常对象
import java.util.List; /* * 异常处理:多个异常对象的处理 * 1、多个异常分别处理 * 2、多个异常一次捕获,多次处理 * 3、多个异常一次捕获一次处理 * */ public class Main{ public static void main(String[] args) { //int []arc=new int[3]; //System.out.println(arc[3]);//ArrayIndexOutOfBoundsException 3 //List<Integer> list = List.of(4, 3, 26, 6); //System.out.println(list.get(4));//IndexOutOfBoundsException /*1.多个异常分别处理 try{ int []arc=new int[3]; System.out.println(arc[3]); }catch(ArrayIndexOutOfBoundsException e) { System.out.println(e); } try{ List<Integer> list = List.of(4, 3, 26, 6); System.out.println(list.get(4)); }catch(IndexOutOfBoundsException e) { System.out.println(e); } java.lang.ArrayIndexOutOfBoundsException: 3 java.lang.IndexOutOfBoundsException: Index 4 out-of-bounds for length 4 */ /*
2.多个异常一次捕获多次处理
注意事项 子类对象必须写在父类对象之上
try{ int []arc=new int[3]; System.out.println(arc[3]); List<Integer> list = List.of(4, 3, 26, 6); System.out.println(list.get(4)); } catch(ArrayIndexOutOfBoundsException e){ System.out.println(e); } catch (IndexOutOfBoundsException e) { System.out.println(e); } } */ /*
3、多个异常一次捕获一次处理
try{ int []arc=new int[3]; System.out.println(arc[3]); List<Integer> list = List.of(4, 3, 26, 6); System.out.println(list.get(4)); } catch(Exception e)