-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReplaceErrorCodeWithException.java
120 lines (105 loc) · 2.76 KB
/
ReplaceErrorCodeWithException.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package ch10;
/*
* A method returns a special code to indicate an error
*
* Throw an exception instead
*
* - Exceptions are better because they clearly separate normal processing
* from error processing. This makes programs easier to understand
*
* - First thing need to decide whether to use a checked or unchecked exception.
* The decision hinges on whether it is the responsibity of the caller to test
* the feature, or the feature's routine. If it is caller's responsibility, then
* should use unchecked exception because it is a programming error, it is a bug.
* Otherwise, should declare the exception in the interface, so the caller can have
* right expectation and to take appropriate measures
*/
class Account {
private int _balance;
public int withdraw(int amount) {
if (amount > _balance)
return -1;
_balance -= amount;
return 0;
}
}
class Client {
public void client() {
if (account.withdraw(amount) == -1) {
handleOverdrawn();
} else {
doTheUsualThing();
}
}
}
// uncheck exception
class AccountRefactored1 {
private int _balance;
public void withdraw(int amount) {
// if (amount > _balance)
// throw new IllegalArgumentException();
// Because it is programming error, can signal more clearly by using assertion
Assert.isTrue("sufficient funds", amount <= _balance);
_balance -= amount;
}
}
class ClientRefactored1 {
public void client() {
// caller do the check
if (!account.canWithdraw(amount)) {
handleOverdrawn();
} else {
account.withdraw(amount);
doTheUsualThing();
}
}
}
// checked exception
class AccountRefactored2 {
private int _balance;
public void withdraw(int amount) throws BalanceException {
if (amount > _balance)
throw new BalanceException();
_balance -= amount;
}
}
class ClientRefactored2 {
public void client() {
try {
account.withdraw(amount);
doTheUsualThing();
} catch (BalanceException e) {
handleOverdrawn();
}
}
}
// checked exception - if the withdraw method been used by tons of callers,
// it is not easy to do it in one step, can create a new method to change step by step
class AccountRefactored3 {
private int _balance;
public int withdraw(int amount) {
try {
newWithdraw(amount);
return 0;
} catch (BalanceException e) {
return -1;
}
}
public void newWithdraw(int amount) throws BalanceException {
if (amount > _balance)
throw new BalanceException();
_balance -= amount;
}
}
class ClientRefactored3 {
public void client() {
try {
account.newWithdraw(amount);
doTheUsualThing();
} catch (BalanceException e) {
handleOverdrawn();
}
}
}
class BalanceException extends Exception {
}