做网站虚拟主机怎么选择,设计素材的网站,新加坡打工,出国游做的好的网站认识异常#xff0c;学会从报错信息中发现问题#xff0c;解决问题。并学会构建自定义异常#xff0c;提醒编程时注意
目录
1.认识异常
2.自定义异常 1.自定义运行时异常
2.自定义编译时异常
3.异常的处理 1.认识异常 异常就是代表程序出现的问题#xff0c;用来查询B… 认识异常学会从报错信息中发现问题解决问题。并学会构建自定义异常提醒编程时注意
目录
1.认识异常
2.自定义异常 1.自定义运行时异常
2.自定义编译时异常
3.异常的处理 1.认识异常 异常就是代表程序出现的问题用来查询BUG关键信息 将错误信息封装为一个错误对象然后提交到JVM虚拟机中返回报错信息显示错误类型错误问题错误位置。 异常类Throwaable 子类包括 Error系统级错误严重问题开发时不理会 Exception异常 我们程序会出现的问题再下设 运行时异常RuntimeException及其子类运行时出现的问题 编译时异常编译阶段会有错误提醒其他异常日期解析异常 2.自定义异常 掌握学习自定义异常和体会作用 1.自定义运行时异常 使用异常对象封装问题并使用throw抛出到上一层经过try catch语句显示 package Exception;public class Ageillega extends RuntimeException {public Ageillega() {}public Ageillega(String message) {super(message);}
}package Exception;public class Text1 {public static void main(String[] args) {try {saveAge(160);System.out.println(底层运行成功);} catch (Exception e) {e.printStackTrace();System.out.println(底层出现问题);}}public static void saveAge(int age){if(age 0 age 150){System.out.println(年龄被成功保存 age);}else{//异常对象封装问题/** 1.定义一个异常类继承RuntimeException* 2.重写构造器* 3.通过throw new 异常类 来创建异常类并抛出编译阶段不报错* */throw new Ageillega(/age is illegal, your age is age);//throw抛出去异常对象}}
}2.自定义编译时异常 还是封装异常对象类提醒更强烈引起注意 throw 是抛出异常对象throws 是用在方法上抛出方法内部的异常 package Exception;public class AgeillegaException extends Exception{public AgeillegaException() {}public AgeillegaException(String message) {super(message);}
}package Exception;public class Text1 {public static void main(String[] args) {try {saveAge(160);System.out.println(底层运行成功);} catch (Exception e) {e.printStackTrace();System.out.println(底层出现问题);}}public static void saveAge2(int age) throws AgeillegaException {if (age 0 age 150) {System.out.println(年龄被成功保存 age);} else {throw new AgeillegaException(/age is illegal, your age is age);//throw抛出去异常对象}}public static void saveAge(int age){if(age 0 age 150){System.out.println(年龄被成功保存 age);}else{throw new Ageillega(/age is illegal, your age is age);//throw抛出去异常对象}}
}3.异常的处理 常见的处理方式: 捕获异常记录异常并响应合适的信息给用户 捕获异常尝试重修修复 解决方法 1. 使用try-catch 语句捕获检测语句错误返回报错信息 2.使用 throws 抛出错误提醒 返回报错信息 学习时间 2024.8.9