throw
和 throws
的区别如下:
throw关键字 | throws关键字 |
---|---|
throw 关键字用于显式抛出异常。 | throws 关键字用于声明异常。 |
已检查的异常不能仅通过 throw 传播。 | 检查的异常可以通过 throws 传播 |
throw 关键字后跟一个实例。 | throws 关键字后面是类。 |
在方法中使用 throw 关键字。 | throws 关键字与方法签名一起使用。 |
不能抛出多个异常 | 可以声明多个异常,例如,public void method() throws IOException, SQLException 。 |