|
| 1 | +# [errs-java][repo-url] [![Maven Central][mvn-img]][mvn-url] [![GitHub.io][io-img]][io-url] [![CI Status][ci-img]][ci-url] [![MIT License][mit-img]][mit-url] |
| 2 | + |
| 3 | +A library for handling exceptions with reasons. |
| 4 | + |
| 5 | +In Java programming, it is cumbersome to implement a separate exception class for each exception case. |
| 6 | +However, trying to handle multiple exception cases with a single exception class makes it difficult to distinguish between them. |
| 7 | + |
| 8 | +The exception class `Exc` provided by this library solves this problem by accepting a `Record` object that represents the reason for the exception. |
| 9 | +Since a `Record` object can have any fields, it can store information about the situation at the time the exception occurred. |
| 10 | +The type of the `Record` object can be determined and cast using a switch statement, making it easy to write handling logic for each exception case. |
| 11 | + |
| 12 | +Optionally, when an `Exc` object is instantiated, pre-registered exception handlers can receive notifications either synchronously or asynchronously. |
| 13 | +However, to enable this feature, you must specify the system property `-Dgithub.sttk.errs.notify=true` at program startup. |
| 14 | + |
| 15 | +## Install |
| 16 | + |
| 17 | +This package can be installed from [Maven Central Repository][mvn-url]. |
| 18 | + |
| 19 | +The examples of declaring that repository and the dependency on this package in Maven `pom.xml` and Gradle `build.gradle` are as follows: |
| 20 | + |
| 21 | +### for Maven |
| 22 | + |
| 23 | +``` |
| 24 | + <dependencies> |
| 25 | + <dependency> |
| 26 | + <groupId>io.github.sttk</groupId> |
| 27 | + <artifactId>errs</artifactId> |
| 28 | + <version>0.1.0</version> |
| 29 | + </dependency> |
| 30 | + </dependencies> |
| 31 | +``` |
| 32 | + |
| 33 | +### for Gradle |
| 34 | + |
| 35 | +``` |
| 36 | +repositories { |
| 37 | + mavenCentral() |
| 38 | +} |
| 39 | +dependencies { |
| 40 | + implementation 'io.github.sttk:errs:0.1.0' |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +## Usage |
| 45 | + |
| 46 | +### Exc instantiation and identification of a reason |
| 47 | + |
| 48 | +The following code instantiates an `Exc` and throws it. |
| 49 | + |
| 50 | +```java |
| 51 | +package sample; |
| 52 | + |
| 53 | +import com.github.sttk.errs.Exc; |
| 54 | + |
| 55 | +public class SampleClass { |
| 56 | + |
| 57 | + record IndexOutOfRange(String name, int index, int min, int max) {} |
| 58 | + |
| 59 | + public void sampleMethod() throws Exc { |
| 60 | + ... |
| 61 | + throw new Exc(new IndexOutOfRange("array", i, 0, array.length)); |
| 62 | + } |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +And the following code catches the exception and identifies the reason with a switch expression. |
| 67 | + |
| 68 | +```java |
| 69 | + try { |
| 70 | + sampleMethod(); |
| 71 | + } catch (Exc e) { |
| 72 | + switch (e.getReason()) { |
| 73 | + case IndexOutOfRange reason -> { |
| 74 | + String name = reason.name(); |
| 75 | + int index = reason.index(); |
| 76 | + int min = reason.min(); |
| 77 | + int max = reason.max(); |
| 78 | + ... |
| 79 | + } |
| 80 | + ... |
| 81 | + } |
| 82 | + } |
| 83 | +``` |
| 84 | + |
| 85 | +### Exception notification (Optional) |
| 86 | + |
| 87 | +> To enable this feature, you must specify the system property `-Dgithub.sttk.errs.notify=true` at program startup. |
| 88 | +
|
| 89 | +This library optionally provides a feature to notify pre-registered exception handlers when an `Exc` is instantiated. |
| 90 | +Multiple exception handlers can be registered, and you can choose to receive notifications either synchronously or asynchronously. |
| 91 | +To register exception handlers that receive notifications synchronously, use the `Exc.AddSyncHandler` static method. |
| 92 | +For asynchronous notifications, use the `Exc.AddAsyncHandler` static method. |
| 93 | + |
| 94 | +Exception notifications will not occur until the `Exc.fixHandlers` static method is called. |
| 95 | +This static method locks the current set of exception handlers, preventing further additions and enabling notification processing. |
| 96 | + |
| 97 | +```java |
| 98 | +package sample; |
| 99 | + |
| 100 | +import com.github.sttk.errs.Exc; |
| 101 | +import static java.time.format.DateTimeFormatter.ISO_INSTANT; |
| 102 | + |
| 103 | +public class Main { |
| 104 | + static { |
| 105 | + Exc.addSyncHandlers((exc, tm) -> { |
| 106 | + System.out.println(String.format("%s - %s:%d", |
| 107 | + exc.getMessage(), exc.getFile(), exc.getLine()); |
| 108 | + }); |
| 109 | + |
| 110 | + Exc.addAsyncHandlers((exc, tm) -> { |
| 111 | + removeLogger.log(String.format("%s:%s:%d:%s", |
| 112 | + tm.format(ISO_INSTANT), exc.getFile(), exc.getLine(), exc.toString())); |
| 113 | + }); |
| 114 | + |
| 115 | + Exc.fixHandlers(); |
| 116 | + } |
| 117 | + |
| 118 | + record IndexOutOfRange(String name, int index, int min, int max) {} |
| 119 | + |
| 120 | + public static void main(String[] args) { |
| 121 | + try { |
| 122 | + throw new Exc(new IndexOutOfRange("array", 11, 0, 10)); |
| 123 | + } catch (Exc e) {} |
| 124 | + } |
| 125 | +} |
| 126 | +``` |
| 127 | + |
| 128 | +```sh |
| 129 | +% java -Dgithub.sttk.errs.notify=true sample.Main |
| 130 | +sample.Main$IndexOutOfRange { name=array, index=11, min=0, max=10 } - Main.java:25 |
| 131 | +``` |
| 132 | + |
| 133 | +## Native build |
| 134 | + |
| 135 | +This library supports native build with GraalVM. |
| 136 | + |
| 137 | +See the following pages to setup native build environment on Linux/macOS or Windows. |
| 138 | +- [Setup native build environment on Linux/macOS](https://www.graalvm.org/latest/reference-manual/native-image/) |
| 139 | +- [Setup native build environment on Windows](https://www.graalvm.org/latest/docs/getting-started/windows/#prerequisites-for-native-image-on-windows) |
| 140 | + |
| 141 | +And see the following pages to build native image with Maven or Gradle. |
| 142 | +- [Native image building with Maven plugin](https://graalvm.github.io/native-build-tools/latest/maven-plugin.html) |
| 143 | +- [Native image building with Gradle plugin](https://graalvm.github.io/native-build-tools/latest/gradle-plugin.html) |
| 144 | + |
| 145 | +**NOTE:** If serialization for `Exc` is used, it is needed to specify the serialization configurations for derived classes of `Record` indicating the reason and derived classes of `Throwable` indicating the causing exception classes in the `serialization-config.json` file. |
| 146 | + |
| 147 | +## Supporting JDK versions |
| 148 | + |
| 149 | +This library supports JDK 21 or later. |
| 150 | + |
| 151 | +### Actually checked JDK versions: |
| 152 | + |
| 153 | +- Oracle GraalVM 24.0.1+9.1 (build 24.0.1+9-jvmci-b01) |
| 154 | +- Oracle GraalVM 23.0.2+7.1 (build 23.0.2+7-jvmci-b01) |
| 155 | +- Oracle GraalVM 21.0.6+8.1 (build 21.0.6+8-LTS-jvmci-23.1-b55) |
| 156 | + |
| 157 | +## License |
| 158 | + |
| 159 | +Copyright (C) 2024 Takayuki Sato |
| 160 | + |
| 161 | +This program is free software under MIT License.<br> |
| 162 | +See the file LICENSE in this distribution for more details. |
| 163 | + |
| 164 | + |
| 165 | +[repo-url]: https://github.com/sttk/errs-java |
| 166 | +[mvn-img]: https://img.shields.io/badge/maven_central-0.1.0-276bdd.svg |
| 167 | +[mvn-url]: https://central.sonatype.com/artifact/io.github.sttk/errs/0.1.0 |
| 168 | +[io-img]: https://img.shields.io/badge/github.io-Javadoc-4d7a97.svg |
| 169 | +[io-url]: https://sttk.github.io/errs-java/ |
| 170 | +[ci-img]: https://github.com/sttk/errs-java/actions/workflows/java-ci.yml/badge.svg?branch=main |
| 171 | +[ci-url]: https://github.com/sttk/errs-java/actions?query=branch%3Amain |
| 172 | +[mit-img]: https://img.shields.io/badge/license-MIT-green.svg |
| 173 | +[mit-url]: https://opensource.org/licenses/MIT |
0 commit comments