https://github.com/qoomon/unchecked-exceptions-java
Throw any Java Exception anywhere without the need of catching them nor wrapping them into RuntimeException
https://github.com/qoomon/unchecked-exceptions-java
checkedexception exception-handling exceptions java rethrow-exceptions throw unchecked-exception wrapping
Last synced: about 1 month ago
JSON representation
Throw any Java Exception anywhere without the need of catching them nor wrapping them into RuntimeException
- Host: GitHub
- URL: https://github.com/qoomon/unchecked-exceptions-java
- Owner: qoomon
- License: mit
- Created: 2018-06-05T10:12:18.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2022-10-30T14:50:22.000Z (over 2 years ago)
- Last Synced: 2025-04-14T23:43:47.123Z (about 1 month ago)
- Topics: checkedexception, exception-handling, exceptions, java, rethrow-exceptions, throw, unchecked-exception, wrapping
- Language: Java
- Homepage:
- Size: 73.2 KB
- Stars: 8
- Watchers: 1
- Forks: 4
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Unchecked Exception
[](https://github.com/qoomon/unchecked-exceptions-java/actions/workflows/build.yml)
[](https://snyk.io/test/github/qoomon/unchecked-exceptions-java)
[](https://codeclimate.com/github/qoomon/unchecked-exceptions-java/maintainability)
[](https://codeclimate.com/github/qoomon/unchecked-exceptions-java/test_coverage)
[](http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22me.qoomon%22%20AND%20a%3A%22unchecked-exceptions%22)
This lib hase **zero dependencies** and can be used to throw checked exceptions without declaring this in your method's head `throws` clause.
And without wrapping `Exception` into a `RuntimeException`. Basically you can throw any `Exception` everywhere without catching them, happy throwing :-D## Methods
`unchecked(Exception)` rethrows any exception as unchecked exception, without wrapping exception.
```java
throw unchecked(checkedException);
````unchecked(LambdaFunction)` returns result or rethrows exception from lambda function as unchecked exception, without wrapping exception.
```java
return unchecked(() -> methodThrowingCheckedException())
```## Usage Examples
### Try-Catch Block
#### Regular Code
```java
import static me.qoomon.UncheckedExceptions.*;public class Example {
void example() {
URL url;
// code polition with try catch block
try {
url = new URL("https:/www.example.org");
} catch (MalformedURLException e) {
// ugly exception wrapping
throw RuntimeException(e);
}
System.out(url);
}
}
```
#### Unchecked Exception Code
```java
import static me.qoomon.UncheckedExceptions.*;public class Example {
void example() {
// get rid of code polition with try catch block and exception wrapping
URL url = unchecked(() -> new URL("https:/www.example.org"));
System.out(url);
}
}
```
### Stream
#### Regular Code
```java
import static me.qoomon.UncheckedExceptions.*;
public class Example {
void example() {
Stream.of("https:/www.example.org")
.map(url -> {
// code polition with try catch block
try {
return new URL(url);
} catch (MalformedURLException e) {
// ugly exception wrapping
throw new RuntimeException(e);
}
});
}
}
```
#### Unchecked Exception Code
```java
import static me.qoomon.UncheckedExceptions.*;
public class Example {
void example() {
Stream.of("https:/www.example.org")
// get rid of code polition with try catch block and exception wrapping
.map(url -> unchecked(() -> new URL(url)));
}
}
```