このコードを実行するたびに、すべて正常に動作しますが、入金メソッドがエラーをスローした場合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");
}
}
}