异常
-
异常代表程序出现的问题
-
error代表系统级别错误,属于严重问题
-
exception,异常,代表我们程序可能出现的问题
-
运行时异常:RuntimeException及其子类,编译阶段不会出现错误提示
-
编译时异常:编译阶段就出现错误提醒
-
package itheima.demo.test; import java.text.SimpleDateFormat; import java.util.Date; public class Exception { public static void main(String[] args) { // 运行时异常特点:编译阶段不报错,运行时出现的异常,继承自 RuntimeException show(); } public static void show1(){ String str="2024-04-02 11:12:13"; SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date=sdf.parse(str);//编译时异常,提醒程序员这里程序很容易出错,要注意 System.out.println(date); } public static void show(){ int[] arr={1,2,3}; // System.out.println(arr[3]);//ArrayIndexOutOfBoundsException //空指针异常 String str=null; System.out.println(str.length());//NullPointerException } }
异常的基本处理
-
抛出异常(throws)
-
在方法上使用throws关键字,可以将方法内部异常抛出去给调用者处理
-
-
捕获异常(try...catch)
-
直接捕获程序出现的异常
-
package itheima.demo.test; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Exception { public static void main(String[] args) { // 运行时异常特点:编译阶段不报错,运行时出现的异常,继承自 RuntimeException try { //监视代码,如果出现异常会被catch拦截住 show1(); } catch (ParseException e) { e.printStackTrace();//打印异常信息 } catch (FileNotFoundException e) { e.printStackTrace(); } } public static void show1() throws ParseException, FileNotFoundException { String str="2024-04-02 11:12:13"; SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); Date date=sdf.parse(str);//编译时异常,提醒程序员这里程序很容易出错,要注意 System.out.println(date); InputStream is=new FileInputStream("D:/meinv.png") } public static void show(){ int[] arr={1,2,3}; // System.out.println(arr[3]);//ArrayIndexOutOfBoundsException //空指针异常 String str=null; System.out.println(str.length());//NullPointerException } }
异常的作用
-
异常是用来定位程序bug的关键信息
-
可以作为方法内部的一种特殊返回值,以便通知上层调用者,方法的执行问题
package itheima.demo.test; public class Exception1 { public static void main(String[] args) { System.out.println("start"); try { System.out.println(div(10,0)); } catch (RuntimeException e) { e.printStackTrace(); } System.out.println("end"); } //求两个数的除的结果,并返回这个结果 public static int div(int a,int b) throws RuntimeException{ if (b==0){ //返回异常给上层调用者,还能告知上层底层是执行成功了还是执行失败了 throw new RuntimeException("除数不能为0"); } int res=a/b; return res; } }
自定义异常
-
Java无法提供所有异常类,所以需要自己来定义异常类
-
自定义编译时异常
定义一个异常类继承Exception 重写构造器 通过throw new 异常类(XX)创建异常并抛出 特点:编译阶段就保错,提醒比较激进
package itheima.demo.test; public class Exception3 { public static void main(String[] args) { try { checkAge(200); } catch (IllegalAgeException e) { e.printStackTrace(); } } // 只要收到年龄小于1岁或者200岁就是一个非法异常 public static void checkAge(int age) throws IllegalAgeException{ if (age<1 || age>200){ throw new IllegalAgeException("年龄不合法"); } else { System.out.println("年龄合法"); } } } package itheima.demo.test; public class IllegalAgeException extends Exception{ public IllegalAgeException(){ } public IllegalAgeException(String message){ super(message); } }
-
自定义运行时异常
定义一个异常类继承RuntimeException 重写构造器 通过throw new 异常类(XX)创建异常并抛出 特点:编译阶段不保错,提醒不属于激进
package itheima.demo.test; public class Exception3 { public static void main(String[] args) { checkAge(500); } // 只要收到年龄小于1岁或者200岁就是一个非法异常 public static void checkAge(int age){ if (age<1 || age>200){ throw new IllegalAgeException("年龄不合法"); } else { System.out.println("年龄合法"); } } } package itheima.demo.test; public class IllegalAgeException extends RuntimeException{ public IllegalAgeException(){ } public IllegalAgeException(String message){ super(message); } }
-
异常的处理方案
-
底层异常层层向上抛出,最外层捕获异常,记录下异常信息,并响应适合用户观看的信息进行提示
public class Exception2 { public static void main(String[] args) { // 运行时异常特点:编译阶段不报错,运行时出现的异常,继承自 RuntimeException try { //监视代码,如果出现异常会被catch拦截住 show1(); } catch (Exception e) { e.printStackTrace();//打印异常信息 } catch (Exception e) { e.printStackTrace(); } } public static void show1() throws Exception { String str = "2024-04-02 11:12:13"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = sdf.parse(str);//编译时异常,提醒程序员这里程序很容易出错,要注意 System.out.println(date); InputStream is = new FileInputStream("D:/meinv.png"); } }
-
最外层捕获异常后,尝试重新修复
package itheima.demo.test; import java.util.Scanner; public class Exception4 { public static void main(String[] args) { while (true) { try { double price = userInoutPrice(); break; } catch (java.lang.Exception e) { e.printStackTrace(); } } System.out.println("ok"); } public static double userInoutPrice(){ Scanner scanner = new Scanner(System.in); System.out.println("请输入价格:"); double price = scanner.nextDouble(); return price; } }