Skip to content

Commit 69d8c10

Browse files
egoebelbeckermaibin
authored andcommitted
BAEL-1044 - Introduction to NoException (eugenp#2412)
* BAEL-1044 - Introduction to NoException * BAEL-1044 - Introduction to NoException * BAEL-1044 - Introduction to NoException * BAEL-1044 - Introduction to NoException * BAEL-1044 - Introduction to NoException * Update pom.xml Add missing </dependency>
1 parent accb430 commit 69d8c10

4 files changed

Lines changed: 94 additions & 1 deletion

File tree

‎libraries/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
- [Introduction to Neuroph](http://www.baeldung.com/intro-to-neuroph)
3030
- [Guide to Apache Commons CircularFifoQueue](http://www.baeldung.com/commons-circular-fifo-queue)
3131
- [Quick Guide to RSS with Rome](http://www.baeldung.com/rome-rss)
32+
- [Introduction to NoException](http://www.baeldung.com/intrduction-to-noexception)
33+
3234

3335
The libraries module contains examples related to small libraries that are relatively easy to use and does not require any separate module of its own.
3436

‎libraries/pom.xml‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,11 @@
462462
<artifactId>pcollections</artifactId>
463463
<version>${pcollections.version}</version>
464464
</dependency>
465+
<dependency>
466+
<groupId>com.machinezoo.noexception</groupId>
467+
<artifactId>noexception</artifactId>
468+
<version>1.1.0</version>
469+
</dependency>
465470
<dependency>
466471
<groupId>org.eclipse.collections</groupId>
467472
<artifactId>eclipse-collections</artifactId>
@@ -510,4 +515,4 @@
510515
<rome.version>1.0</rome.version>
511516
<eclipse-collections.version>8.2.0</eclipse-collections.version>
512517
</properties>
513-
</project>
518+
</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: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
Exceptions.log().run(() -> System.out.println("Result is " + Integer.parseInt("foobar")));
24+
}
25+
26+
@Test
27+
public void givenLogger_whenDefaultNoException_thenCatchAndLogWithClassName() {
28+
Exceptions.log(logger).run(() -> System.out.println("Result is " + Integer.parseInt("foobar")));
29+
}
30+
31+
@Test
32+
public void givenLoggerAndMessage_whenDefaultNoException_thenCatchAndLogWithMessage() {
33+
Exceptions.log(logger, "Something went wrong:").run(() -> System.out.println("Result is " + Integer.parseInt("foobar")));
34+
}
35+
36+
@Test
37+
public void givenDefaultValue_whenDefaultNoException_thenCatchAndLogPrintDefault() {
38+
System.out.println("Result is " + Exceptions.log(logger, "Something went wrong:").get(() -> Integer.parseInt("foobar")).orElse(-1));
39+
}
40+
41+
@Test(expected = Error.class)
42+
public void givenCustomHandler_whenError_thenRethrowError() {
43+
CustomExceptionHandler customExceptionHandler = new CustomExceptionHandler();
44+
customExceptionHandler.run(() -> throwError());
45+
}
46+
47+
@Test
48+
public void givenCustomHandler_whenException_thenCatchAndLog() {
49+
CustomExceptionHandler customExceptionHandler = new CustomExceptionHandler();
50+
customExceptionHandler.run(() -> throwException());
51+
}
52+
53+
private static void throwError() {
54+
throw new Error("This is very bad.");
55+
}
56+
57+
private static void throwException() {
58+
String testString = "foo";
59+
testString.charAt(5);
60+
}
61+
62+
}

0 commit comments

Comments
 (0)