-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptionHandlingExample2.java
More file actions
37 lines (32 loc) · 935 Bytes
/
ExceptionHandlingExample2.java
File metadata and controls
37 lines (32 loc) · 935 Bytes
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
package concept.examples.exceptionhandling;
//PLEASE GIVE ME A BREAK ON CODING STANDARDS
class Amount {
public Amount(String currency, int amount) {
this.currency = currency;
this.amount = amount;
}
String currency;
int amount;// Should ideally use BigDecimal
}
class CurrenciesDoNotMatchException extends RuntimeException {
}
class AmountAdder {
static Amount addAmounts(Amount amount1, Amount amount2) {
if (!amount1.currency.equals(amount2.currency)) {
throw new CurrenciesDoNotMatchException();
}
return new Amount(amount1.currency, amount1.amount + amount2.amount);
}
}
public class ExceptionHandlingExample2 {
public static void main(String[] args) {
try {
AmountAdder.addAmounts(new Amount("RUPEE", 5), new Amount("RUPEE",
5));
String string = null;
string.toString();
} catch (CurrenciesDoNotMatchException e) {
System.out.println("Handled CurrenciesDoNotMatchException");
}
}
}