每次我运行此代码时,一切正常,但如果存款方法抛出错误,则只有main 方法中的 捕获异常并打印字符串,catch
尽管 catch
. ExceptionsDemo
为什么会这样?
public class ExceptionsDemo {
public static void show() throws IOException {
var account = new Account();
try {
account.deposit(0);//this method might throw an IOException
account.withDraw(2);
} catch (InsufficientFundsException e) {
System.out.println(e.getMessage());
}
}
}
public class Main {
public static void main(String[] args) {
try {
ExceptionsDemo.show();
} catch (IOException e) {
System.out.println("An unexpected error occurred");
}
}
}