Java throw和throws区别
throw和throws关键字之间有很多区别。掷球与掷球之间的差异列表如下:
throw |
throws |
Java throw关键字用于显式引发异常。 |
Java throws关键字用于声明异常。 |
已检查的异常不能仅使用throw传播。 |
受检查的异常可以通过抛出来传播。 |
抛出一个实例。 |
投掷之后是类。 |
方法中使用了投掷。 |
Throws与方法签名一起使用。 |
您不能抛出多个异常。 |
您可以声明多个异常,例如 public void method()引发IOException,SQLException。 |
Java throw示例
void m(){
throw new ArithmeticException("sorry");
}
Java throws示例
void m()throws ArithmeticException{
//method code}
Java throws示例
void m()throws ArithmeticException{
throw new ArithmeticException("sorry");
}