{"id":23788730,"url":"https://github.com/suppierk/java-throwable-utils","last_synced_at":"2025-09-06T07:32:15.079Z","repository":{"id":119995780,"uuid":"260452345","full_name":"SuppieRK/java-throwable-utils","owner":"SuppieRK","description":"Java extensions allowing to use checked exceptions in functional programming API + Try","archived":false,"fork":false,"pushed_at":"2024-12-21T13:30:07.000Z","size":215,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-12-21T14:29:31.213Z","etag":null,"topics":["exceptions","functions","java","lambdas","throwable","try"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SuppieRK.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-05-01T12:19:44.000Z","updated_at":"2024-12-21T13:30:09.000Z","dependencies_parsed_at":null,"dependency_job_id":"fd348871-c411-4c56-8ba6-c1600a0bea66","html_url":"https://github.com/SuppieRK/java-throwable-utils","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SuppieRK%2Fjava-throwable-utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SuppieRK%2Fjava-throwable-utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SuppieRK%2Fjava-throwable-utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SuppieRK%2Fjava-throwable-utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SuppieRK","download_url":"https://codeload.github.com/SuppieRK/java-throwable-utils/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":232104443,"owners_count":18473163,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["exceptions","functions","java","lambdas","throwable","try"],"created_at":"2025-01-01T16:48:04.220Z","updated_at":"2025-09-06T07:32:15.070Z","avatar_url":"https://github.com/SuppieRK.png","language":"Java","readme":"# Java Throwable Utils\n\nThis dependency-less library serves for one simple purpose:\nreduce boilerplate try-catch statements during work with Java Stream API.\n\n[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2FSuppieRK%2Fjava-throwable-utils.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2FSuppieRK%2Fjava-throwable-utils?ref=badge_shield)\n\n[![SonarCloud](https://sonarcloud.io/images/project_badges/sonarcloud-orange.svg)](https://sonarcloud.io/summary/overall?id=SuppieRK_java-throwable-utils)\n\n## How to add\n\n- Maven\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003eio.github.suppierk\u003c/groupId\u003e\n    \u003cartifactId\u003ejava-throwable-utils\u003c/artifactId\u003e\n    \u003cversion\u003e2.0.1\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n- Gradle\n\n```groovy\ndependencies {\n    implementation 'io.github.suppierk:java-throwable-utils:2.0.1'\n}\n```\n\n## Examples\n\nIf you had to use constructs like:\n\n```java\nimport java.util.ArrayList;\nimport java.util.List;\n\npublic class Demo {\n    public static String throwingMethod(String source) throws Exception {\n        throw new Exception(source);\n    }\n\n    public static void main(String[] args) {\n        List\u003cString\u003e test = new ArrayList\u003c\u003e();\n        test.add(\"sample\");\n\n        try {\n            test.stream()\n                    .map(s -\u003e {\n                        try {\n                            return throwingMethod(s);\n                        } catch (Exception e) {\n                            // Here we would have to:\n                            // a) Return some value to filter out later and log exception\n                            // b) Wrap and rethrow an exception to catch it later again\n                            throw new RuntimeException(e);\n                        }\n                    })\n                    .forEach(s -\u003e {\n                        try {\n                            throwingMethod(s);\n                        } catch (Exception e) {\n                            // Here we would have to:\n                            // a) Suppress and log exception\n                            // b) Wrap and rethrow an exception to catch it later again\n                            throw new RuntimeException(e);\n                        }\n                    });\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n    }\n}\n``` \n\nwith this library, you can simplify this pipeline to:\n\n```java\nimport io.github.suppierk.java.util.function.*;\n\nimport java.util.ArrayList;\nimport java.util.List;\n\npublic class Demo {\n    public static String throwingMethod(String source) throws Exception {\n        throw new Exception(source);\n    }\n\n    public static void main(String[] args) {\n        List\u003cString\u003e test = new ArrayList\u003c\u003e();\n        test.add(\"sample\");\n\n        try {\n            test.stream()\n                    .map((ThrowableFunction\u003cString, String\u003e) Demo::throwingMethod)\n                    .forEach((ThrowableConsumer\u003cString\u003e) Demo::throwingMethod);\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n    }\n}\n```\n\nand you can take it further and define functions explicitly, removing the need to specify function types:\n\n```java\nimport io.github.suppierk.java.util.function.*;\n\nimport java.util.ArrayList;\nimport java.util.List;\n\npublic class Demo {\n    public static ThrowableFunction\u003cString, String\u003e throwingMap() {\n        return source -\u003e {\n            throw new Exception(source);\n        };\n    }\n\n    public static ThrowableConsumer\u003cString\u003e throwingConsumer() {\n        return source -\u003e {\n            throw new Exception(source);\n        };\n    }\n\n    public static void main(String[] args) {\n        List\u003cString\u003e test = new ArrayList\u003c\u003e();\n        test.add(\"sample\");\n\n        try {\n            test.stream().map(Demo.throwingMap()).forEach(Demo.throwingConsumer());\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n    }\n}\n```\n\nor with the help of `UnsafeFunctions` utility class you can shorten it even more without changing your logic too much:\n\n\u003e This is the recommended, the least intrusive and the least verbose way\n\n```java\nimport static io.github.suppierk.java.UnsafeFunctions.*;\n\nimport java.util.ArrayList;\nimport java.util.List;\n\npublic class Demo {\n    public static String throwingMethod(String source) throws Exception {\n        throw new Exception(source);\n    }\n\n    public static void main(String[] args) {\n        List\u003cString\u003e test = new ArrayList\u003c\u003e();\n        test.add(\"sample\");\n\n        try {\n            test.stream()\n                    .map(unsafeFunction(Demo::throwingMethod))\n                    .forEach(unsafeConsumer(Demo::throwingMethod));\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n    }\n}\n```\n\nAll exceptions will be propagated using neat trick similar to Apache Commons `ExceptionUtils.rethrow` by leveraging Java\ntype erasure to make checked exceptions unchecked.\n\n## Try\n\nThis library has simple implementation of `Try`, which benefits greatly from presence of these functions\nand allows us to handle exceptions in functional style much like you deal with nullable values using `Optional`.\n\n```java\nimport io.github.suppierk.java.Try;\n\nimport java.util.ArrayList;\nimport java.util.List;\nimport java.util.stream.Collectors;\n\npublic class Test {\n    public String throwingMethod(String source) throws Exception {\n        throw new Exception(source);\n    }\n\n    public void processingMethod() {\n        List\u003cString\u003e test = new ArrayList\u003c\u003e();\n\n        test.stream()\n                .map(s -\u003e Try.of(() -\u003e throwingMethod(s)))\n                .filter(Try::isSuccess)\n                .collect(Collectors.toList());\n    }\n}\n```\n\nSame as for `Optional`, `Try` in a case of failure will preserve only first exception happened in a call chain and skip\nfurther operations.\n\n## License\n\n[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2FSuppieRK%2Fjava-throwable-utils.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2FSuppieRK%2Fjava-throwable-utils?ref=badge_large)","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsuppierk%2Fjava-throwable-utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsuppierk%2Fjava-throwable-utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsuppierk%2Fjava-throwable-utils/lists"}