声明异常:
throws,不处理异常,丢给调用者处理

public static void f() throws IOException{ ...}丢给调用方法处理public static void main(String[]args) throws IOException{ f();}main()方法丢给jre处理

自定义异常:
在运行时定义的异常用throw,如果是编译器异常则需要throws或try-catch,比如继承的是Exception

Person p=new Person(); p.setAge(-1);class Person{ private int age; public void setAge(int age) { if(age<0) { throw new ill("年龄不能为负数"); } } public int getAge() { return age; }}class ill extends RuntimeException{ //继承RuntimeException public ill() { } public ill(String s) { super(s); //使用父类的构造类方法 }结果为:年龄不能为负数