BAEL-1044 - Introduction to NoException (#2394)
* BAEL-1044 - Introduction to NoException * BAEL-1044 - Introduction to NoException
This commit is contained in:
parent
415b80d03f
commit
2036138ed1
|
@ -0,0 +1,2 @@
|
||||||
|
### Relevant Articles:
|
||||||
|
- [Introduction to NoException](http://www.baeldung.com/intrduction-to-noexception)
|
|
@ -0,0 +1,23 @@
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>noexception</artifactId>
|
||||||
|
<version>1.0</version>
|
||||||
|
<name>noexception</name>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-modules</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.machinezoo.noexception</groupId>
|
||||||
|
<artifactId>noexception</artifactId>
|
||||||
|
<version>1.1.0</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,63 @@
|
||||||
|
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() {
|
||||||
|
System.out.println("Result is " + Exceptions.log(logger).get(() -> +Integer.parseInt("foobar")).orElse(-1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenLoggerAndMessage_whenDefaultNoException_thenCatchAndLogWithClassNameAndMessage() {
|
||||||
|
System.out.println("Result is " + Exceptions.log(logger, "Something went wrong:").get(() -> +Integer.parseInt("foobar")).orElse(-1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue