出现异常后,要么抛throws 要么 try catch
- thrwos 。 是抛给了你的上一级(调用者)。这个异常我处理不了或者不知道怎么处理时使用。
- try catch代码块 。是自己处理异常,不用再抛出去,因为自己都能处理的异常抛出去没意义了。
例子:下面代码1/0会抛出ArithmeticException ,被catch语句捕获并进行处理,所以不需要再在方法头再throws异常。
public void test() { // throws ArithmeticException
try {
System.out.println(1 / 0);
} catch (ArithmeticException e) {
System.out.println(e.toString());
}
}
1523

被折叠的 条评论
为什么被折叠?



