Skip to content

Commit 30b4394

Browse files
mrudav-shuklapedja4
authored andcommitted
BAEL-1921 - added example code and unit test for GlobalExceptionHandler (eugenp#4646)
* added example code and unit test for GlobalExceptionHandler * Code for common exceptions
1 parent f3237ef commit 30b4394

14 files changed

Lines changed: 407 additions & 0 deletions
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.exceptions;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
public class Arithmetic {
7+
8+
private static Logger LOGGER = LoggerFactory.getLogger(Arithmetic.class);
9+
10+
public static void main(String[] args) {
11+
12+
try {
13+
int result = 30 / 0; // Trying to divide by zero
14+
} catch (ArithmeticException e) {
15+
LOGGER.error("ArithmeticException caught!");
16+
}
17+
18+
}
19+
20+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.exceptions;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
public class ArrayIndexOutOfBounds {
7+
8+
private static Logger LOGGER = LoggerFactory.getLogger(ArrayIndexOutOfBounds.class);
9+
10+
public static void main(String[] args) {
11+
12+
int[] nums = new int[] { 1, 2, 3 };
13+
14+
try {
15+
int numFromNegativeIndex = nums[-1]; // Trying to access at negative index
16+
int numFromGreaterIndex = nums[4]; // Trying to access at greater index
17+
int numFromLengthIndex = nums[3]; // Trying to access at index equal to size of the array
18+
} catch (ArrayIndexOutOfBoundsException e) {
19+
LOGGER.error("ArrayIndexOutOfBoundsException caught");
20+
}
21+
22+
}
23+
24+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.exceptions;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
class Animal {
7+
8+
}
9+
10+
class Dog extends Animal {
11+
12+
}
13+
14+
class Lion extends Animal {
15+
16+
}
17+
18+
public class ClassCast {
19+
20+
private static Logger LOGGER = LoggerFactory.getLogger(ClassCast.class);
21+
22+
public static void main(String[] args) {
23+
24+
try {
25+
Animal animalOne = new Dog(); // At runtime the instance is dog
26+
Dog bruno = (Dog) animalOne; // Downcasting
27+
28+
Animal animalTwo = new Lion(); // At runtime the instance is animal
29+
Dog tommy = (Dog) animalTwo; // Downcasting
30+
} catch (ClassCastException e) {
31+
LOGGER.error("ClassCastException caught!");
32+
}
33+
34+
}
35+
36+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.exceptions;
2+
3+
import java.io.BufferedReader;
4+
import java.io.File;
5+
import java.io.FileNotFoundException;
6+
import java.io.FileReader;
7+
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
11+
public class FileNotFound {
12+
13+
private static Logger LOGGER = LoggerFactory.getLogger(FileNotFound.class);
14+
15+
public static void main(String[] args) {
16+
17+
BufferedReader reader = null;
18+
try {
19+
reader = new BufferedReader(new FileReader(new File("/invalid/file/location")));
20+
} catch (FileNotFoundException e) {
21+
LOGGER.error("FileNotFoundException caught!");
22+
}
23+
}
24+
25+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.exceptions;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
public class GlobalExceptionHandler {
7+
8+
public static void main(String[] args) {
9+
10+
Handler globalExceptionHandler = new Handler();
11+
Thread.setDefaultUncaughtExceptionHandler(globalExceptionHandler);
12+
new GlobalExceptionHandler().performArithmeticOperation(10, 0);
13+
}
14+
15+
public int performArithmeticOperation(int num1, int num2) {
16+
return num1/num2;
17+
}
18+
19+
}
20+
21+
class Handler implements Thread.UncaughtExceptionHandler {
22+
23+
private static Logger LOGGER = LoggerFactory.getLogger(Handler.class);
24+
25+
public void uncaughtException(Thread t, Throwable e) {
26+
LOGGER.info("Unhandled exception caught!");
27+
}
28+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.exceptions;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
public class IllegalArgument {
7+
8+
private static Logger LOGGER = LoggerFactory.getLogger(IllegalArgument.class);
9+
10+
public static void main(String[] args) {
11+
try {
12+
Thread.sleep(-1000);
13+
} catch (InterruptedException e) {
14+
LOGGER.error("IllegalArgumentException caught!");
15+
}
16+
}
17+
18+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.exceptions;
2+
3+
import java.util.ArrayList;
4+
import java.util.Iterator;
5+
import java.util.List;
6+
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
10+
public class IllegalState {
11+
12+
private static Logger LOGGER = LoggerFactory.getLogger(IllegalState.class);
13+
14+
public static void main(String[] args) {
15+
16+
List<Integer> intList = new ArrayList<>();
17+
18+
for (int i = 0; i < 10; i++) {
19+
intList.add(i);
20+
}
21+
22+
Iterator<Integer> intListIterator = intList.iterator(); // Initialized with index at -1
23+
24+
try {
25+
intListIterator.remove(); // IllegalStateException
26+
} catch (IllegalStateException e) {
27+
LOGGER.error("IllegalStateException caught!");
28+
}
29+
30+
}
31+
32+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.exceptions;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
class ChildThread extends Thread {
7+
8+
private static Logger LOGGER = LoggerFactory.getLogger(ChildThread.class);
9+
10+
public void run() {
11+
try {
12+
Thread.sleep(1000);
13+
} catch (InterruptedException e) {
14+
LOGGER.error("InterruptedException caught!");
15+
}
16+
}
17+
18+
}
19+
20+
public class InterruptedExceptionExample {
21+
22+
public static void main(String[] args) throws InterruptedException {
23+
ChildThread childThread = new ChildThread();
24+
childThread.start();
25+
childThread.interrupt();
26+
}
27+
28+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.exceptions;
2+
3+
import java.net.MalformedURLException;
4+
import java.net.URL;
5+
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
9+
public class MalformedURL {
10+
11+
private static Logger LOGGER = LoggerFactory.getLogger(MalformedURL.class);
12+
13+
public static void main(String[] args) {
14+
15+
URL baeldungURL = null;
16+
17+
try {
18+
baeldungURL = new URL("malformedurl");
19+
} catch (MalformedURLException e) {
20+
LOGGER.error("MalformedURLException caught!");
21+
}
22+
23+
}
24+
25+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.exceptions;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
public class NullPointer {
7+
8+
private static Logger LOGGER = LoggerFactory.getLogger(NullPointer.class);
9+
10+
public static void main(String[] args) {
11+
12+
Person personObj = null;
13+
14+
try {
15+
String name = personObj.personName; // Accessing the field of a null object
16+
personObj.personName = "Jon Doe"; // Modifying the field of a null object
17+
} catch (NullPointerException e) {
18+
LOGGER.error("NullPointerException caught!");
19+
}
20+
21+
}
22+
}
23+
24+
class Person {
25+
26+
public String personName;
27+
28+
public String getPersonName() {
29+
return personName;
30+
}
31+
32+
public void setPersonName(String personName) {
33+
this.personName = personName;
34+
}
35+
36+
}

0 commit comments

Comments
 (0)