
Throwable是异常的顶层父类,代表所有的非正常情况。它有两个直接子类,分别是Error、Exception。
Error是错误,一般是指与虚拟机相关的问题,如系统崩溃、虚拟机错误、动态链接失败等,这种错误无法恢复或不可能捕获,将导致应用程序中断。通常应用程序无法处理这些错误,因此应用程序不应该试图使用catch块来捕获Error对象。在定义方法时,也无须在其throws子句中声明该方法可能抛出Error及其任何子类。
Exception是异常,它被分为两大类,分别是Checked异常和Runtime异常。所有的RuntimeException类及其子类的实例被称为Runtime异常;不是RuntimeException类及其子类的异常实例则被称为Checked异常。Java认为Checked异常都是可以被处理(修复)的异常,所以Java程序必须显式处理Checked异常。如果程序没有处理Checked异常,该程序在编译时就会发生错误,无法通过编译。Runtime异常则更加灵活,Runtime异常无须显式声明抛出,如果程序需要捕获Runtime异常,也可以使用try...catch块来实现。
自定义运行时的异常捕获
public class AgeillegalException extends RuntimeException {
public AgeillegalException() {
}
public AgeillegalException(String message) {
super(message);
}
}
public class Test { public static void main(String[] args) { try { SavaAge(180); System.out.println("底层运行成功"); }catch (Exception e){ e.printStackTrace(); System.out.println("底层出现了bug"); } } public static void SavaAge(int age){ if (age>0 && age<120){ System.out.println("年龄被保存成功"+age); }else { throw new AgeillegalException("age is false,you age is"+age); } } }
自定义编译时的异常捕获
public class AgeillegalException2 extends Exception { public AgeillegalException2() { } public AgeillegalException2(String message) { super(message); } }
public class Test2 { public static void main(String[] args) { try { SavaAge(180); System.out.println("底层运行成功"); }catch (Exception e){ e.printStackTrace(); System.out.println("底层出现了bug"); } } public static void SavaAge(int age) throws AgeillegalException2{ //throw 跑出去这个异常对象 throws用在方法上,抛出方法内部的异常对象 if (age>0 && age<120){ System.out.println("年龄被保存成功"+age); }else { throw new AgeillegalException2("age is false,you age is"+age); } } }
当这个异常影响较大,非常容易出现时,建议选择编译时异常捕获提醒。
当这个异常影响较小时,建议选择运行时异常捕获提醒。
异常的处理方式
1.捕获异常,记录异常,并相应合适的信息给用户
public class ExceptionHandle {
public static void main(String[] args) {
try {
test1();
}catch (FileNotFoundException e){
e.printStackTrace();
}catch (ParseException e){
e.printStackTrace();
}
}
public static void test1() throws ParseException,FileNotFoundException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = sdf.parse("2028-11-11 10:24:25");
System.out.println(d);
test2();
}
public static void test2() throws FileNotFoundException {
InputStream is = new FileInputStream("D:meinv.png");
}
}
处理异常的写法优化
public class ExceptionHandle { public static void main(String[] args) { try { test1(); }catch (Exception e){ 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:25"); System.out.println(d); test2(); } public static void test2() throws Exception { InputStream is = new FileInputStream("D:meinv.png"); } }
2.捕获异常,尝试重新修复
public class Test23 {
public static void main(String[] args) {
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("您输入的价格不合适");
}
}
}
}

2304

被折叠的 条评论
为什么被折叠?



