Skip to content

Commit 34b9860

Browse files
committed
add alternative solutions
1 parent 7e5d1fc commit 34b9860

3 files changed

Lines changed: 27 additions & 0 deletions

File tree

Exercise.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,31 @@ public static void main(String[] args) {
2525
System.err.println(e.getMessage());
2626
}
2727

28+
// alternative multiple catch:
29+
try {
30+
e1.setSalaryInEuro(55000);
31+
e2.setSalaryInEuro(77000);
32+
e3.setSalaryInEuro(45000);
33+
} catch (SalaryDecreaseException e) {
34+
System.out.println("Das neue Gehalt muss hoeher sein als das bisherige");
35+
} catch (SalaryIncreaseTooHighException e) {
36+
System.out.println("Das neue Gehalt darf maximal 10% ueber dem bisherigen Gehalt liegen");
37+
}
38+
39+
// alternative instance of
40+
try {
41+
e1.setSalaryInEuro(55000);
42+
e2.setSalaryInEuro(77000);
43+
e3.setSalaryInEuro(45000);
44+
} catch (Exception e) {
45+
if (e instanceof SalaryDecreaseException) {
46+
System.out.println("Das neue Gehalt muss hoeher sein als das bisherige");
47+
}
48+
if (e instanceof SalaryIncreaseTooHighException) {
49+
System.out.println("Das neue Gehalt darf maximal 10% ueber dem bisherigen Gehalt liegen");
50+
}
51+
}
52+
2853
System.out.println(company.toString());
2954
}
3055
}

SalaryDecreaseException.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ public class SalaryDecreaseException extends Exception {
22

33
public SalaryDecreaseException() {
44
super("Das neue Gehalt muss hoeher sein als das bisherige");
5+
// alternativ: super();
56
}
67
}

SalaryIncreaseTooHighException.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ public class SalaryIncreaseTooHighException extends Exception {
22

33
public SalaryIncreaseTooHighException() {
44
super("Das neue Gehalt darf maximal 10% ueber dem bisherigen Gehalt liegen");
5+
// alternativ: super();
56
}
67
}

0 commit comments

Comments
 (0)