Skip to content

Commit 2036138

Browse files
egoebelbeckermaibin
authored andcommitted
BAEL-1044 - Introduction to NoException (eugenp#2394)
* BAEL-1044 - Introduction to NoException * BAEL-1044 - Introduction to NoException
1 parent 415b80d commit 2036138

5 files changed

Lines changed: 113 additions & 0 deletions

File tree

‎noexception/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
### Relevant Articles:
2+
- [Introduction to NoException](http://www.baeldung.com/intrduction-to-noexception)

‎noexception/pom.xml‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.baeldung</groupId>
6+
<artifactId>noexception</artifactId>
7+
<version>1.0</version>
8+
<name>noexception</name>
9+
10+
<parent>
11+
<groupId>com.baeldung</groupId>
12+
<artifactId>parent-modules</artifactId>
13+
<version>1.0.0-SNAPSHOT</version>
14+
</parent>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>com.machinezoo.noexception</groupId>
19+
<artifactId>noexception</artifactId>
20+
<version>1.1.0</version>
21+
</dependency>
22+
</dependencies>
23+
</project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.noexception;
2+
3+
import com.machinezoo.noexception.ExceptionHandler;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
7+
public class CustomExceptionHandler extends ExceptionHandler {
8+
9+
private Logger logger = LoggerFactory.getLogger(CustomExceptionHandler.class);
10+
11+
@Override
12+
public boolean handle(Throwable throwable) {
13+
14+
if (throwable.getClass()
15+
.isAssignableFrom(RuntimeException.class)
16+
|| throwable.getClass()
17+
.isAssignableFrom(Error.class)) {
18+
return false;
19+
} else {
20+
logger.error("Caught Exception ", throwable);
21+
return true;
22+
}
23+
}
24+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.baeldung.noexception;
2+
3+
import com.machinezoo.noexception.Exceptions;
4+
import org.junit.Test;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
public class NoExceptionUnitTest {
9+
10+
private static Logger logger = LoggerFactory.getLogger(NoExceptionUnitTest.class);
11+
12+
@Test
13+
public void whenStdExceptionHandling_thenCatchAndLog() {
14+
try {
15+
System.out.println("Result is " + Integer.parseInt("foobar"));
16+
} catch (Throwable exception) {
17+
logger.error("Caught exception:", exception);
18+
}
19+
}
20+
21+
@Test
22+
public void whenDefaultNoException_thenCatchAndLog() {
23+
24+
Exceptions.log().run(() -> System.out.println("Result is " + Integer.parseInt("foobar")));
25+
}
26+
27+
@Test
28+
public void givenLogger_whenDefaultNoException_thenCatchAndLogWithClassName() {
29+
System.out.println("Result is " + Exceptions.log(logger).get(() -> +Integer.parseInt("foobar")).orElse(-1));
30+
}
31+
32+
@Test
33+
public void givenLoggerAndMessage_whenDefaultNoException_thenCatchAndLogWithClassNameAndMessage() {
34+
System.out.println("Result is " + Exceptions.log(logger, "Something went wrong:").get(() -> +Integer.parseInt("foobar")).orElse(-1));
35+
}
36+
37+
@Test
38+
public void givenDefaultValue_whenDefaultNoException_thenCatchAndLogPrintDefault() {
39+
System.out.println("Result is " + Exceptions.log(logger, "Something went wrong:").get(() -> +Integer.parseInt("foobar")).orElse(-1));
40+
}
41+
42+
@Test(expected = Error.class)
43+
public void givenCustomHandler_whenError_thenRethrowError() {
44+
CustomExceptionHandler customExceptionHandler = new CustomExceptionHandler();
45+
customExceptionHandler.run(() -> throwError());
46+
}
47+
48+
@Test
49+
public void givenCustomHandler_whenException_thenCatchAndLog() {
50+
CustomExceptionHandler customExceptionHandler = new CustomExceptionHandler();
51+
customExceptionHandler.run(() -> throwException());
52+
}
53+
54+
private static void throwError() {
55+
throw new Error("This is very bad.");
56+
}
57+
58+
private static void throwException() {
59+
String testString = "foo";
60+
testString.charAt(5);
61+
}
62+
63+
}

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
<module>mockito2</module>
107107
<module>mocks</module>
108108
<module>mustache</module>
109+
<module>noexception</module>
109110

110111
<module>orika</module>
111112

0 commit comments

Comments
 (0)