Skip to content

Commit 7b8bc69

Browse files
authored
Merge pull request eugenp#1006 from sbmaggarwal/master
Added chainedexceptions in core-java
2 parents 2c522ca + b2a03d9 commit 7b8bc69

7 files changed

Lines changed: 143 additions & 0 deletions

File tree

‎core-java/.gitignore‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,7 @@
1616
*.txt
1717
/bin/
1818
/temp
19+
20+
#IntelliJ specific
21+
.idea
22+
*.iml
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.chainedexception;
2+
3+
import com.baeldung.chainedexception.exceptions.GirlFriendOfManagerUpsetException;
4+
import com.baeldung.chainedexception.exceptions.ManagerUpsetException;
5+
import com.baeldung.chainedexception.exceptions.NoLeaveGrantedException;
6+
import com.baeldung.chainedexception.exceptions.TeamLeadUpsetException;
7+
8+
public class LogWithChain {
9+
10+
public static void main(String[] args) throws Exception {
11+
getLeave();
12+
}
13+
14+
private static void getLeave() throws NoLeaveGrantedException {
15+
try {
16+
howIsTeamLead();
17+
} catch (TeamLeadUpsetException e) {
18+
throw new NoLeaveGrantedException("Leave not sanctioned.", e);
19+
}
20+
}
21+
22+
private static void howIsTeamLead() throws TeamLeadUpsetException {
23+
try {
24+
howIsManager();
25+
} catch (ManagerUpsetException e) {
26+
throw new TeamLeadUpsetException(
27+
"Team lead is not in good mood", e);
28+
}
29+
}
30+
31+
private static void howIsManager() throws ManagerUpsetException {
32+
try {
33+
howIsGirlFriendOfManager();
34+
} catch (GirlFriendOfManagerUpsetException e) {
35+
throw new ManagerUpsetException("Manager is in bad mood", e);
36+
}
37+
}
38+
39+
private static void howIsGirlFriendOfManager()
40+
throws GirlFriendOfManagerUpsetException {
41+
throw new GirlFriendOfManagerUpsetException(
42+
"Girl friend of manager is in bad mood");
43+
}
44+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.baeldung.chainedexception;
2+
3+
import com.baeldung.chainedexception.exceptions.GirlFriendOfManagerUpsetException;
4+
import com.baeldung.chainedexception.exceptions.ManagerUpsetException;
5+
import com.baeldung.chainedexception.exceptions.NoLeaveGrantedException;
6+
import com.baeldung.chainedexception.exceptions.TeamLeadUpsetException;
7+
8+
public class LogWithoutChain {
9+
10+
public static void main(String[] args) throws Exception {
11+
getLeave();
12+
}
13+
14+
private static void getLeave() throws NoLeaveGrantedException {
15+
try {
16+
howIsTeamLead();
17+
} catch (TeamLeadUpsetException e) {
18+
e.printStackTrace();
19+
throw new NoLeaveGrantedException("Leave not sanctioned.");
20+
}
21+
}
22+
23+
private static void howIsTeamLead() throws TeamLeadUpsetException {
24+
try {
25+
howIsManager();
26+
} catch (ManagerUpsetException e) {
27+
e.printStackTrace();
28+
throw new TeamLeadUpsetException(
29+
"Team lead is not in good mood");
30+
}
31+
}
32+
33+
private static void howIsManager() throws ManagerUpsetException {
34+
try {
35+
howIsGirlFriendOfManager();
36+
} catch (GirlFriendOfManagerUpsetException e) {
37+
e.printStackTrace();
38+
throw new ManagerUpsetException("Manager is in bad mood");
39+
}
40+
}
41+
42+
private static void howIsGirlFriendOfManager()
43+
throws GirlFriendOfManagerUpsetException {
44+
throw new GirlFriendOfManagerUpsetException(
45+
"Girl friend of manager is in bad mood");
46+
}
47+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.chainedexception.exceptions;
2+
3+
public class GirlFriendOfManagerUpsetException extends Exception {
4+
5+
public GirlFriendOfManagerUpsetException(String message, Throwable cause) {
6+
super(message, cause);
7+
}
8+
9+
public GirlFriendOfManagerUpsetException(String message) {
10+
super(message);
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.chainedexception.exceptions;
2+
3+
public class ManagerUpsetException extends Exception {
4+
5+
public ManagerUpsetException(String message, Throwable cause) {
6+
super(message, cause);
7+
}
8+
9+
public ManagerUpsetException(String message) {
10+
super(message);
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.chainedexception.exceptions;
2+
3+
public class NoLeaveGrantedException extends Exception {
4+
5+
public NoLeaveGrantedException(String message, Throwable cause) {
6+
super(message, cause);
7+
}
8+
9+
public NoLeaveGrantedException(String message) {
10+
super(message);
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.chainedexception.exceptions;
2+
3+
public class TeamLeadUpsetException extends Exception {
4+
5+
public TeamLeadUpsetException(String message, Throwable cause) {
6+
super(message, cause);
7+
}
8+
9+
public TeamLeadUpsetException(String message) {
10+
super(message);
11+
}
12+
}

0 commit comments

Comments
 (0)