目录

1.异常是什么?
2.异常的代表是谁?分为几类?
Error :代表的系统级别错误(属于严重问题),也就是说系统一旦出现问题, s u n 公司会把这些问题封装成 Error 对象给出来,说白了, Error 是给 s u n 公司自己用的,不是给我们程序员用的,因此我们开发人员不用管它。
Exception :叫异常,它代表的才是我们程序可能出现的问题,所以,我们程序员通常会用 Exception 以及它的孩子来封装程序出现的问题。
.运行时异常: RuntimeException 及其子类,编译阶段不会出现错误提醒,运行时出现的异常(如:数组索引越界异常)
.编译时异常:编译阶段就会出现错误提醒的。(如:日期解析异常)
3.异常代码层面的处理有几种方式?

- package com.xinbao.d3_exception;
-
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
-
- public class Exception {
- public static void main(String[] args) throws ParseException {
- // try {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- Date d = sdf.parse("2028-11-11 10:24");
- System.out.println(d);
- // } catch (ParseException e) {
- // e.printStackTrace();
- // }
- }
- }
- E:\JVsoft\Java\jdk-17\bin\java.exe -javaagent:E:\JVsoft\IntelliJIDEA2021.1.1\lib\idea_rt.jar=61424:E:\JVsoft\IntelliJIDEA2021.1.1\bin -Dfile.encoding=UTF-8 -classpath E:\JVsoft\api-app3-3\out\production\api-app3-3 com.xinbao.d3_exception.Exception
- Exception in thread "main" java.text.ParseException: Unparseable date: "2028-11-11 10:24"
- at java.base/java.text.DateFormat.parse(DateFormat.java:399)
- at com.xinbao.d3_exception.Exception.main(Exception.java:11)
-
- 进程已结束,退出代码为 1
. Java 无法为这个世界上全部的问题都提供异常类来代表,如果企业自己的某种问题,想通过异常来表示,以便用异常来管理该问题,那就需要自己来定义异常类了。
异常有什么作用?
1、异常是用来查寻系统 Bug 的关键参考信息!
2、异常可以作为方法内部的一种特殊返回值,以便诵知上层调用者底层的执行情况!

- package com.xinbao.d3_exception;
- //年龄非法运行时异常
- public class AgeIllegalRuntimeException extends RuntimeException{
- //必须让这个类继承自RuntimeException,才能成为异常类
-
- //重写构造器
- public AgeIllegalRuntimeException() {
- }
-
- public AgeIllegalRuntimeException(String message) {
- super(message);
- }
- }
- package com.xinbao.d3_exception;
-
- public class ExceptionTest2 {
- public static void main(String[] args) {
- //需求:保存一个合法年龄
- try {
- saveAge(88);
- saveAge(188);
- System.out.println("底层执行成功!");
- } catch (java.lang.Exception e) {
- e.printStackTrace();
- System.out.println("底层出现bug");
- }
- }
-
- public static void saveAge(int age){
- if (age>0 && age<150){
- System.out.println("年龄保存成功" + age);
- }else {
- //用一个异常对象封装这个问题
- //throw 抛出这个异常对象
- throw new AgeIllegalRuntimeException("/age is illegal, yoyr age is " + age);
- }
- }
- }
- E:\JVsoft\Java\jdk-17\bin\java.exe -javaagent:E:\JVsoft\IntelliJIDEA2021.1.1\lib\idea_rt.jar=61657:E:\JVsoft\IntelliJIDEA2021.1.1\bin -Dfile.encoding=UTF-8 -classpath E:\JVsoft\api-app3-3\out\production\api-app3-3 com.xinbao.d3_exception.ExceptionTest2
- 年龄保存成功88
- 底层出现bug
- com.xinbao.d3_exception.AgeIllegalRuntimeException: /age is illegal, yoyr age is 188
- at com.xinbao.d3_exception.ExceptionTest2.saveAge(ExceptionTest2.java:22)
- at com.xinbao.d3_exception.ExceptionTest2.main(ExceptionTest2.java:8)
-
- 进程已结束,退出代码为 0
编译时异常
- package com.xinbao.d3_exception;
-
- public class AgeIllegalException extends Exception {
- public AgeIllegalException() {
- }
-
- public AgeIllegalException(String message) {
- super(message);
- }
- }
- package com.xinbao.d3_exception;
-
- public class ExceptionTest2 {
- public static void main(String[] args) {
- //需求:保存一个合法年龄
- try {
- // saveAge(88);
- // saveAge(188);
- saveAge2(355);
- System.out.println("底层执行成功!");
- } catch (java.lang.Exception e) {
- e.printStackTrace();
- System.out.println("底层出现bug");
- }
- }
-
- public static void saveAge(int age){
- if (age>0 && age<150){
- System.out.println("年龄保存成功" + age);
- }else {
- //用一个异常对象封装这个问题
- //throw 抛出这个异常对象
- throw new AgeIllegalRuntimeException("/age is illegal, yoyr age is " + age);
- }
- }
-
- public static void saveAge2(int age) throws AgeIllegalException{
- if (age>0 && age<150){
- System.out.println("年龄保存成功" + age);
- }else {
- //用一个异常对象封装这个问题
- //throw 抛出这个异常对象
- //throws 用在方法上 抛出方法内部的异常
- throw new AgeIllegalException("/age is illegal, yoyr age is " + age);
- }
- }
- }
- E:\JVsoft\Java\jdk-17\bin\java.exe -javaagent:E:\JVsoft\IntelliJIDEA2021.1.1\lib\idea_rt.jar=61734:E:\JVsoft\IntelliJIDEA2021.1.1\bin -Dfile.encoding=UTF-8 -classpath E:\JVsoft\api-app3-3\out\production\api-app3-3 com.xinbao.d3_exception.ExceptionTest2
- com.xinbao.d3_exception.AgeIllegalException: /age is illegal, yoyr age is 355
- at com.xinbao.d3_exception.ExceptionTest2.saveAge2(ExceptionTest2.java:34)
- at com.xinbao.d3_exception.ExceptionTest2.main(ExceptionTest2.java:9)
- 底层出现bug
-
- 进程已结束,退出代码为 0

捕获异常,记录异常并相应合适的信息给用户
- package com.xinbao.d3_exception;
-
- 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 Exceptiontest3 {
- public static void main(String[] args) {
- // try {
- // test1();
- // } catch (ParseException e) {
- //
- // e.printStackTrace();//打印出这个异常对象的信息,记录下来
- // System.out.println("日期异常");
- // } catch (FileNotFoundException e) {
- // System.out.println("文件读取异常");
- // e.printStackTrace();
- // }
-
- try {
- test1();
- } catch (Exception e) {
- System.out.println("您当前操作有问题~");
- e.printStackTrace();
- }
- }
-
- //public static void test1() throws ParseException, FileNotFoundException {
- public static void test1() throws Exception {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
- Date d = sdf.parse("2023-10-27 21:59");//如果有异常,直接抛出异常,不会再往下执行
- System.out.println(d);
- test2();
- }
-
- //public static void test2() throws FileNotFoundException {
- public static void test2() throws Exception {
- //读取文件
- InputStream is = new FileInputStream("E:\\社会实践\\社会实践报告(1).docx");
- }
- }
- E:\JVsoft\Java\jdk-17\bin\java.exe -javaagent:E:\JVsoft\IntelliJIDEA2021.1.1\lib\idea_rt.jar=35733:E:\JVsoft\IntelliJIDEA2021.1.1\bin -Dfile.encoding=UTF-8 -classpath E:\JVsoft\api-app3-3\out\production\api-app3-3 com.xinbao.d3_exception.Exceptiontest3
- Fri Oct 27 21:59:00 CST 2023
-
- 进程已结束,退出代码为 0
捕获异常,尝试重新修复
- package com.xinbao.d3_exception;
-
- import java.util.Scanner;
-
- public class ExceptionTest4 {
- public static void main(String[] args) {
- //需求:调用一个方法,让用户输入一个合适的价格返回为止
- //重新修复
- while (true) {
- try {
- getMoney();
- break;
- } catch (Exception e) {
- //e.printStackTrace();
- }
- }
- }
-
- public static double getMoney(){
- Scanner sc = new Scanner(System.in);
- System.out.println("put in money:");
- while (true) {
- double money = sc.nextDouble();
- if (money >= 0){
- return money;
- }else {
- System.out.println("input again:");
- }
- }
- }
- }
- E:\JVsoft\Java\jdk-17\bin\java.exe -javaagent:E:\JVsoft\IntelliJIDEA2021.1.1\lib\idea_rt.jar=35800:E:\JVsoft\IntelliJIDEA2021.1.1\bin -Dfile.encoding=UTF-8 -classpath E:\JVsoft\api-app3-3\out\production\api-app3-3 com.xinbao.d3_exception.ExceptionTest4
- put in money:
- -89
- input again:
- joaj
- put in money:
- vnmio
- put in money:
- 24123
-
- 进程已结束,退出代码为 0