Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Challenge: Bank Account Withdrawal | Custom Exception Types
C++ Exception Handling

bookChallenge: Bank Account Withdrawal

Challenge Overview:

You will build a simple BankAccount class in C++ that supports withdrawing funds. If a withdrawal request exceeds the available balance, your code must throw a custom exception named InsufficientFundsException. This exercise helps you practice creating and using your own exception types to handle error conditions in a realistic scenario.

Key Requirements:

  • Define a custom exception type, InsufficientFundsException, that inherits from std::exception and overrides the what() method to return the message "Insufficient funds".
  • Implement a BankAccount class with:
    • A constructor to set the initial balance;
    • A withdraw(double amount) method that throws an InsufficientFundsException if amount is greater than the current balance; otherwise, it subtracts amount from the balance.

Example usage:

BankAccount account(100.0);
account.withdraw(50.0); // OK
account.withdraw(60.0); // Throws InsufficientFundsException
Aufgabe

Swipe to start coding

Build two classes to simulate a bank account withdrawal scenario with custom exception handling:

  • Define a custom exception type that inherits from std::exception and overrides the what() method to return a specific message when an error occurs.
  • Create a class to represent a bank account with a balance. Include a constructor for setting the initial balance and a method for withdrawing funds. If the withdrawal amount exceeds the available balance, your method must throw your custom exception; otherwise, it should deduct the amount from the balance.

Focus on using clear class and method names. Ensure your exception message is easy to identify. Follow best practices for exception handling and class design.

Lösung

solution.cpp

solution.cpp

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 3
single

single

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Suggested prompts:

Can you show me how to define the InsufficientFundsException class?

How should I implement the withdraw method to throw the exception?

Can you explain how exception handling works in this scenario?

close

Awesome!

Completion rate improved to 6.67

bookChallenge: Bank Account Withdrawal

Swipe um das Menü anzuzeigen

Challenge Overview:

You will build a simple BankAccount class in C++ that supports withdrawing funds. If a withdrawal request exceeds the available balance, your code must throw a custom exception named InsufficientFundsException. This exercise helps you practice creating and using your own exception types to handle error conditions in a realistic scenario.

Key Requirements:

  • Define a custom exception type, InsufficientFundsException, that inherits from std::exception and overrides the what() method to return the message "Insufficient funds".
  • Implement a BankAccount class with:
    • A constructor to set the initial balance;
    • A withdraw(double amount) method that throws an InsufficientFundsException if amount is greater than the current balance; otherwise, it subtracts amount from the balance.

Example usage:

BankAccount account(100.0);
account.withdraw(50.0); // OK
account.withdraw(60.0); // Throws InsufficientFundsException
Aufgabe

Swipe to start coding

Build two classes to simulate a bank account withdrawal scenario with custom exception handling:

  • Define a custom exception type that inherits from std::exception and overrides the what() method to return a specific message when an error occurs.
  • Create a class to represent a bank account with a balance. Include a constructor for setting the initial balance and a method for withdrawing funds. If the withdrawal amount exceeds the available balance, your method must throw your custom exception; otherwise, it should deduct the amount from the balance.

Focus on using clear class and method names. Ensure your exception message is easy to identify. Follow best practices for exception handling and class design.

Lösung

solution.cpp

solution.cpp

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 3
single

single

some-alt