BAEL-1044 - Introduction to NoException (#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>
This commit is contained in:
Eric Goebelbecker 2017-08-10 13:59:38 -04:00 committed by maibin
parent accb430f5e
commit 69d8c10751
4 changed files with 94 additions and 1 deletions

View File

@ -29,6 +29,8 @@
- [Introduction to Neuroph](http://www.baeldung.com/intro-to-neuroph) - [Introduction to Neuroph](http://www.baeldung.com/intro-to-neuroph)
- [Guide to Apache Commons CircularFifoQueue](http://www.baeldung.com/commons-circular-fifo-queue) - [Guide to Apache Commons CircularFifoQueue](http://www.baeldung.com/commons-circular-fifo-queue)
- [Quick Guide to RSS with Rome](http://www.baeldung.com/rome-rss) - [Quick Guide to RSS with Rome](http://www.baeldung.com/rome-rss)
- [Introduction to NoException](http://www.baeldung.com/intrduction-to-noexception)
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. 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.

View File

@ -462,6 +462,11 @@
<artifactId>pcollections</artifactId> <artifactId>pcollections</artifactId>
<version>${pcollections.version}</version> <version>${pcollections.version}</version>
</dependency> </dependency>
<dependency>
<groupId>com.machinezoo.noexception</groupId>
<artifactId>noexception</artifactId>
<version>1.1.0</version>
</dependency>
<dependency> <dependency>
<groupId>org.eclipse.collections</groupId> <groupId>org.eclipse.collections</groupId>
<artifactId>eclipse-collections</artifactId> <artifactId>eclipse-collections</artifactId>
@ -510,4 +515,4 @@
<rome.version>1.0</rome.version> <rome.version>1.0</rome.version>
<eclipse-collections.version>8.2.0</eclipse-collections.version> <eclipse-collections.version>8.2.0</eclipse-collections.version>
</properties> </properties>
</project> </project>

View File

@ -0,0 +1,24 @@
package com.baeldung.noexception;
import com.machinezoo.noexception.ExceptionHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class CustomExceptionHandler extends ExceptionHandler {
private Logger logger = LoggerFactory.getLogger(CustomExceptionHandler.class);
@Override
public boolean handle(Throwable throwable) {
if (throwable.getClass()
.isAssignableFrom(RuntimeException.class)
|| throwable.getClass()
.isAssignableFrom(Error.class)) {
return false;
} else {
logger.error("Caught Exception ", throwable);
return true;
}
}
}

View File

@ -0,0 +1,62 @@
package com.baeldung.noexception;
import com.machinezoo.noexception.Exceptions;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class NoExceptionUnitTest {
private static Logger logger = LoggerFactory.getLogger(NoExceptionUnitTest.class);
@Test
public void whenStdExceptionHandling_thenCatchAndLog() {
try {
System.out.println("Result is " + Integer.parseInt("foobar"));
} catch (Throwable exception) {
logger.error("Caught exception:", exception);
}
}
@Test
public void whenDefaultNoException_thenCatchAndLog() {
Exceptions.log().run(() -> System.out.println("Result is " + Integer.parseInt("foobar")));
}
@Test
public void givenLogger_whenDefaultNoException_thenCatchAndLogWithClassName() {
Exceptions.log(logger).run(() -> System.out.println("Result is " + Integer.parseInt("foobar")));
}
@Test
public void givenLoggerAndMessage_whenDefaultNoException_thenCatchAndLogWithMessage() {
Exceptions.log(logger, "Something went wrong:").run(() -> System.out.println("Result is " + Integer.parseInt("foobar")));
}
@Test
public void givenDefaultValue_whenDefaultNoException_thenCatchAndLogPrintDefault() {
System.out.println("Result is " + Exceptions.log(logger, "Something went wrong:").get(() -> Integer.parseInt("foobar")).orElse(-1));
}
@Test(expected = Error.class)
public void givenCustomHandler_whenError_thenRethrowError() {
CustomExceptionHandler customExceptionHandler = new CustomExceptionHandler();
customExceptionHandler.run(() -> throwError());
}
@Test
public void givenCustomHandler_whenException_thenCatchAndLog() {
CustomExceptionHandler customExceptionHandler = new CustomExceptionHandler();
customExceptionHandler.run(() -> throwException());
}
private static void throwError() {
throw new Error("This is very bad.");
}
private static void throwException() {
String testString = "foo";
testString.charAt(5);
}
}