Run ❯
Get your
own
website
❯
Run Code
Ctrl+Alt+R
Change Orientation
Ctrl+Alt+O
Change Theme
Ctrl+Alt+D
Change Language
Go to Spaces
Ctrl+Alt+P
class InsufficientFundsException implements Exception { final double balance; final double amount; InsufficientFundsException(this.balance, this.amount); @override String toString() => 'InsufficientFundsException: tried to take $amount, but the balance is $balance'; } class BankAccount { double balance = 0; void deposit(double amount) { balance += amount; } void withdraw(double amount) { if (amount > balance) { throw InsufficientFundsException(balance, amount); } balance -= amount; } } void main() { var account = BankAccount(); account.deposit(100); try { account.withdraw(30); print('Balance: ${account.balance}'); account.withdraw(500); print('Balance: ${account.balance}'); } on InsufficientFundsException catch (e) { print(e); print('You need ${e.amount - e.balance} more'); } }
Balance: 70.0
InsufficientFundsException: tried to take 500.0, but the balance is 70.0
You need 430.0 more