// Una excepción sube por las llamadas hasta que alguien la captura.
// Con el argumento "crash", main no la captura y el programa termina.
public class Propagation {

    static void charge(BankAccount account, long amount) throws InsufficientFundsException {
        IO.println("  charge: retira " + amount);
        account.withdraw(amount);
        IO.println("  charge: listo");          // no se ejecuta si withdraw lanza
    }

    static void processOrder(BankAccount account, long total) throws InsufficientFundsException {
        IO.println(" processOrder: total " + total);
        charge(account, total);
        IO.println(" processOrder: pedido pagado");
    }

    public static void main(String[] args) throws InsufficientFundsException {
        BankAccount account = new BankAccount("C-01");
        account.deposit(500);
        if (args.length > 0 && args[0].equals("crash")) {
            processOrder(account, 800);
            return;
        }
        try {
            processOrder(account, 300);
            processOrder(account, 800);
            IO.println("main: no llega aquí");
        } catch (InsufficientFundsException e) {
            IO.println("main: " + e.getMessage());
            IO.println("main: faltan " + (e.requested() - e.balance()) + " céntimos");
        }
        IO.println("main: saldo final " + account.balance());
    }
}
