Guest User

Untitled

a guest
Jan 21st, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. public Object doSomething() throws Throwable {
  2. ...
  3. }
  4.  
  5. public Object doSomething() throws BusinessException {
  6.  
  7. if(!hasMinimumbalance())
  8. {
  9. throw new InsufficientBalance(ErrorCode);
  10. }
  11. }
  12.  
  13. public void myMethod() throws Throwable {
  14. if (x) {
  15. throw new MyException1();
  16. }
  17. if (y) {
  18. throw new MyException2();
  19. }
  20. }
  21.  
  22. public void callingMethod() {
  23. try {
  24. myMethod();
  25. }
  26. catch(Throwable t) {
  27. if (t instanceof MyException1) {
  28. // handle exception 1
  29. }
  30. else if (t instanceof MyException2) {
  31. // handle exception 2
  32. }
  33. else {
  34. // handle other exceptions
  35. }
  36. }
  37. }
  38.  
  39. public void myMethod() throws MyException1, MyException2 {
  40. if (x) {
  41. throw new MyException1();
  42. }
  43. if (y) {
  44. throw new MyException2();
  45. }
  46. }
  47.  
  48. public void callingMethod() {
  49. try {
  50. myMethod();
  51. }
  52. catch(MyException1 e) {
  53. // handle exception 1
  54. }
  55. catch(MyException2 e) {
  56. // handle exception 2
  57. }
  58. }
  59.  
  60. prvate int parseAmount(String amountValue) {
  61. int amount;
  62. try {
  63. amount = Integer.parseInt(amountValue);
  64. }
  65. catch(NumberFormatException e) {
  66. // default amount
  67. amount = 0;
  68. }
  69. return amount;
  70. }
  71.  
  72. private Customer getCustomer(int customerId) throws ServiceException {
  73. try {
  74. return customerService.getCustomer(customerId);
  75. }
  76. catch(CustomerServiceSpaghettiTangledException e) {
  77. throw new ServiceException("Error calling the customer service", e);
  78. }
  79. }
Add Comment
Please, Sign In to add comment