就是代表程序中出现的问题
//把数字字符串转变为整型: Integer.valueOf("123"); 正常执行
// 若这样写: Integer.valueOf("abc");是错误的,出现运行时异常
// int[] arr = {11, 22, 33};
// System.out.println(arr[5]); 运行时异常
(main方法是由JVM虚拟机调的,所以JVM虚拟机是main方法的最上层程序)
以上述异常为例,在方法的位置上写throws ParseException
public class ExceptionTest1 {
public static void main(String[] args) throws ParseException{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = sdf.parse("2028-11-11 10:24");
System.out.println(d);
}
}
1,定义一个异常类:即先写一个类,异常叫什么这个类的名字就叫什么
如:年龄非法参数异常
// 1、必须让这个类继承自RuntimeException,才能成为一个运行时异常类。
public class AgeIllegalRuntimeException extends RuntimeException{
//右键选Generate,找构造器,一般选择前两个
public AgeIllegalRuntimeException() {
}
public AgeIllegalRuntimeException(String message) { //有参构造器,用来封装当前异常出现的原因
super(message); //原因收到后送到了父类public RuntimeException(String message)这个有参构造器接收,就是把这个问题交给父类封装
}
}
2,主程序:
public class ExceptionTest2 {
public static void main(String[] args) {
// 需求:保存一个合法的年龄
try {
saveAge(223);
System.out.println("底层执行成功的!");
} catch (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, your age is " + age);
}
}
}
1,定义一个编译时异常类:
// 1、必须让这个类继承自Exception,才能成为一个编译时异常类。
public class AgeIllegalException extends Exception{
public AgeIllegalException() {
}
public AgeIllegalException(String message) {
super(message);
}
}
2.主程序:
public class ExceptionTest2 {
public static void main(String[] args) {
// 需求:保存一个合法的年龄
try {
saveAge2(225);
System.out.println("saveAge2底层执行是成功的!");
} catch (AgeIllegalException e) {
e.printStackTrace();
System.out.println("saveAge2底层执行是出现bug的!");
}
}
public static void saveAge2(int age) throws AgeIllegalException{
if(age > 0 && age < 150){
System.out.println("年龄被成功保存: " + age);
}else {
// 用一个异常对象封装这个问题
// throws 用在方法上,抛出方法内部的异常
throw new AgeIllegalException("/age is illegal, your age is " + age);
}
}
}
问题严重,用编译时异常
问题不严重,一般不会出现,抛运行时异常
1,一般在最外层捕获异常,记录异常并--------
public static void main(String[] args) {
try { //最外层进行捕获处理
test1(); //调test1方法
} catch (Exception e) {
System.out.println("您当前操作有问题");
e.printStackTrace(); // 打印出这个异常对象的信息。记录下来。
}
}
public static void test1() throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = sdf.parse("2028-11-11 10:24:11");
System.out.println(d);
test2(); //test1方法调test2方法
}
public static void test2() throws Exception {
// 读取文件的。
InputStream is = new FileInputStream("D:/meinv.png");
}
2,捕获异常,尝试重新修复
public class ExceptionTest4 {
public static void main(String[] args) {
// 需求:调用一个方法,让用户输入一个合适的价格返回为止。
// while死循环 尝试修复
while (true) {
try {
System.out.println(getMoney());
break;
} catch (Exception e) {
System.out.println("请您输入合法的数字!!");
}
}
}
public static double getMoney(){
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请您输入合适的价格:");
double money = sc.nextDouble();
if(money >= 0){
return money;
}else {
System.out.println("您输入的价格是不合适的!");
}
}
}
}