Skip to content

A wrapper for a Try-catch block in Java, providing useful methods to obtain values from the block

License

Notifications You must be signed in to change notification settings

cbrt-x/try-catch-wrapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Download via Jitpack

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>
<dependency>
    <groupId>com.github.Jadefalke256</groupId>
    <artifactId>Try-Catch-Wrapper</artifactId>
    <version>v1.0.1</version>
</dependency>

Usage

Run method

Try.attempt(this::executeMethod)
	.onCatch(Throwable::printStacktrace)
	.run();

Obtain value from method call

var files = Try.attempt(() -> Files.list(path))
    .onCatch(IOException.class, Throwable::printStacktrace)
    .obtain();

files.ifPresent(this::doSomething);

Use finally

Try.attempt(this::startProcess)
    .onFinally(this::endProcess);
    .run();