forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
1 parent
accb430
commit 69d8c10
Showing
4 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
libraries/src/main/java/com/baeldung/noexception/CustomExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
libraries/src/test/java/com/baeldung/noexception/NoExceptionUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |