Java Exception


SUBMITTED BY: Guest

DATE: Jan. 14, 2014, 7:19 p.m.

FORMAT: Java

SIZE: 1.1 kB

HITS: 18533

  1. class LowBalanceException extends Throwable
  2. {
  3. public String toString()
  4. {
  5. return("Low Balance");
  6. }
  7. }
  8. class TransactionExceededException extends Throwable
  9. {
  10. public String toString()
  11. {
  12. return("Transaction Exceeded");
  13. }
  14. }
  15. class Account
  16. {
  17. void withdraw(double balance, double amount) throws LowBalanceException, TransactionExceededException
  18. {
  19. double bal = balance - amount;
  20. if (bal < 500)
  21. {
  22. throw new LowBalanceException();
  23. }
  24. if (amount > 20000)
  25. {
  26. throw new TransactionExceededException();
  27. }
  28. if (bal >=500 && amount < 20000)
  29. {
  30. System.out.println("New Balance = " + bal);
  31. }
  32. }
  33. }
  34. class Bank
  35. {
  36. public static void main(String ar[])
  37. {
  38. Account a = new Account();
  39. try
  40. {
  41. a.withdraw(20000,1000);
  42. a.withdraw(10000,9700);
  43. a.withdraw(30000,26000);
  44. }
  45. catch(LowBalanceException e1)
  46. {
  47. System.out.println(e1);
  48. }
  49. catch(TransactionExceededException e2)
  50. {
  51. System.out.println(e2);
  52. }
  53. }
  54. }

comments powered by Disqus