📜  Java中的throw和throws之间的区别

📅  最后修改于: 2020-09-26 08:18:23             🧑  作者: Mango

Java中的throw和throws之间的区别

throw和throws关键字之间有很多区别。抛出与抛出之间的差异列表如下:

No. throw throws
1) Java throw keyword is used to explicitly throw an exception. Java throws keyword is used to declare an exception.
2) Checked exception cannot be propagated using throw only. Checked exception can be propagated with throws.
3) Throw is followed by an instance. Throws is followed by class.
4) Throw is used within the method. Throws is used with the method signature.
5) You cannot throw multiple exceptions. You can declare multiple exceptions e.g.
public void method()throws IOException,SQLException.

Java抛出示例

void m(){
throw new ArithmeticException("sorry");
}

Java抛出例子

void m()throws ArithmeticException{
//method code
}

JavaThrows(异常对象)示例

void m()throws ArithmeticException{
throw new ArithmeticException("sorry");
}