Skip to content

8.5 Custom Exceptions (throw vs throws)

🎯 Lesson Objectives

  • Custom Exception कब और क्यों बनाना चाहिए।
  • throw (एक्सेप्शन फेंकना) बनाम throws (मेथड सिग्नेचर में घोषणा करना)।

⚙️ Code: Custom InvalidAgeException

// 1. कस्टम अनचेक्ड एक्सेप्शन क्लास
class InsufficientFundsException extends RuntimeException {
    public InsufficientFundsException(String message) {
        super(message);
    }
}

public class BankWallet {
    private double balance = 1000.0;

    // throws: कॉलर को चेतावनी देना
    public void withdraw(double amount) {
        if (amount > balance) {
            // throw: नया एक्सेप्शन फेंकना
            throw new InsufficientFundsException("अपर्याप्त बैलेंस! आपके पास केवल ₹" + balance + " हैं।");
        }
        balance -= amount;
        System.out.println("₹" + amount + " निकाले गए। शेष: ₹" + balance);
    }

    public static void main(String[] args) {
        BankWallet wallet = new BankWallet();
        try {
            wallet.withdraw(2500.0);
        } catch (InsufficientFundsException ex) {
            System.out.println("ट्रांजैक्शन फेल: " + ex.getMessage());
        }
    }
}

🧭 Navigation

Last updated on