Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue: #64 ------------------------------------------------------------------------ On behalf of the community, the JUnit Lambda Team thanks msg systems ag (http://www.msg-systems.com) for supporting the JUnit crowdfunding campaign! ------------------------------------------------------------------------
- Loading branch information
Showing
4 changed files
with
81 additions
and
47 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
junit-commons/src/main/java/org/junit/gen5/commons/util/ExceptionUtils.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,46 @@ | ||
/* | ||
* Copyright 2015-2016 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v1.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.junit.gen5.commons.util; | ||
|
||
/** | ||
* Collection of utilities for working with exceptions. | ||
* | ||
* @since 5.0 | ||
*/ | ||
public final class ExceptionUtils { | ||
|
||
/** | ||
* Throw the supplied {@link Throwable}, <em>masked</em> as a | ||
* {@link RuntimeException}. | ||
* | ||
* <p>The supplied {@code Throwable} will not be wrapped. Rather, it | ||
* will be thrown as-is using a hack based on generics and type erasure | ||
* that tricks the Java compiler into believing that the thrown exception | ||
* is an unchecked exception. | ||
* | ||
* @param t the Throwable to throw as a {@code RuntimeException} | ||
* @return this method always throws an exception and therefore never | ||
* returns anything; the return type is merely present to allow this | ||
* method to be supplied as the operand in a {@code throw} statement | ||
*/ | ||
public static RuntimeException throwAsRuntimeException(Throwable t) { | ||
ExceptionUtils.<RuntimeException> throwAs(t); | ||
|
||
// Appeasing the compiler: the following line will never be executed. | ||
return null; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
sbrannen
Author
Member
|
||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private static <T extends Throwable> void throwAs(Throwable t) throws T { | ||
throw (T) t; | ||
} | ||
|
||
} |
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
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
ExceptionUtils.throwAsUncheckedException(t)
emits the following warning in IntelliJand
throw ExceptionUtils.throwAsUncheckedException(t)
also emits a warningTherefore following recommendation: switch to
throw new AssertionError("Appeasing the compiler: this will never be executed");