OpenJ / Libraries / DysfxMake failure explicit

Make failure a value you can handle.

Dysfx is a small Java library for the missing monad: represent success and failure as data, choose how checked exceptions are converted, and match the result without throwing it through every caller.

Turn exceptions into values

Try captures a successful computation or a failure and lets the caller decide what to do next:

Try<Integer> number = Try.of(() -> Integer.parseInt("four"));
int value = number.getOrElse(3);

// value == 3
>> returns a Failure value, then uses 3 as the fallback

Choose what gets caught

ThrowControl makes checked-exception policy explicit. A caller can convert standard checked exceptions into Failure while allowing errors or selected exceptions to escape.

Try.ofCheckedSupplier(supply,
    ThrowControl.ofStandardChecked());

Keep the useful failure

Either is for operations where the left side is not necessarily an exception. A parser can return the original input or the parsed value, so recovery still has the information that caused the failure.

Either<String, Integer> result =
    new Right<>(Integer.parseInt("40"));

assertTrue(result.isRight());
assertTrue(result.contains(40));

Unlike Try, the left side can preserve a useful domain value instead of an exception:

String input = "not-a-number";
Either<String, Integer> parsed =
    new Left<>(input);

// the failed input is still available for recovery

Add Dysfx to a project

The published core module is io.teknek.dysfx:core:0.0.5. See it on Maven Central, inspect the core POM, or browse the source repository.

Maven:

<dependency>
  <groupId>io.teknek.dysfx</groupId>
  <artifactId>core</artifactId>
  <version>0.0.5</version>
</dependency>

Gradle:

dependencies {
    implementation("io.teknek.dysfx:core:0.0.5")
}

Useful when: Java code needs explicit failure paths without burying recovery in nested try/catch blocks.