运行时异常继承自RuntimeException;
package foundationEnhance; public class Person { private int age; public Person(int age) { super(); if(age<0){ throw new ageException("年龄不可以为负数!"); } this.age = age; } public int getAge() { return age; } public void setAge(int age) { if(age<0){ throw new ageException("年龄不可以为负数!"); } this.age = age; } }
package foundationEnhance; public class testPerson { public static void main(String[] args) { Person p=new Person(-1); p.setAge(-2); System.out.println(p.getAge()); } }
package foundationEnhance; public class ageException extends RuntimeException{ public ageException() { } public ageException(String message) { super(message); } }
编译时异常继承自Exception;
看看前面的视频,关于运行时异常和编译时异常;
...未完待补充
原文地址:https://www.cnblogs.com/shijinglu2018/p/11111847.html
时间: 2024-11-03 22:44:28