美文网首页
异常处理细节02

异常处理细节02

作者: 哈迪斯Java | 来源:发表于2021-11-11 15:46 被阅读0次

package chapter12.Exception_;

public class Demon03 {
public static void main(String[] args) {
//如果try代码块有多个异常
//可以使用多个catch,分别捕获不同的内容
//但是要求子类异常写在前面,父类异常写在后面
try {
Person person = new Person();
person=null;
person.getName();//空指针异常
System.out.println(person.getName());
int a =21;
int b=0;
int res=a/b;//算术异常
} catch (NumberFormatException e){
System.out.println("空指针异常="+e.getMessage());
}
catch (ArithmeticException e){
System.out.println("算术异常="+e.getMessage());
}
catch (Exception e) {
e.printStackTrace();
} finally {
}
}
}
class Person{
private String name="jack";

public String getName() {
    return name;
}

}

相关文章

网友评论

      本文标题:异常处理细节02

      本文链接:https://www.haomeiwen.com/subject/wwvizltx.html