https://github.com/rogerkeays/unchecked
Skip to content Toggle navigation
Sign up
* Product
+
Actions
Automate any workflow
+
Packages
Host and manage packages
+
Security
Find and fix vulnerabilities
+
Codespaces
Instant dev environments
+
Copilot
Write better code with AI
+
Code review
Manage code changes
+
Issues
Plan and track work
+
Discussions
Collaborate outside of code
Explore
+ All features
+ Documentation
+ GitHub Skills
+ Blog
* Solutions
For
+ Enterprise
+ Teams
+ Startups
+ Education
By Solution
+ CI/CD & Automation
+ DevOps
+ DevSecOps
Case Studies
+ Customer Stories
+ Resources
* Open Source
+
GitHub Sponsors
Fund open source developers
+
The ReadME Project
GitHub community articles
Repositories
+ Topics
+ Trending
+ Collections
* Pricing
Search or jump to...
Search code, repositories, users, issues, pull requests...
Search
[ ]
Clear
Search syntax tips
Provide feedback
We read every piece of feedback, and take your input very seriously.
[ ] [ ] Include my email address so I can be
contacted
Cancel Submit feedback
Saved searches
Use saved searches to filter your results more quickly
Name [ ]
Query [ ]
To see all available qualifiers, see our documentation.
Cancel Create saved search
Sign in
Sign up
You signed in with another tab or window. Reload to refresh your
session. You signed out in another tab or window. Reload to refresh
your session. You switched accounts on another tab or window. Reload
to refresh your session.
{{ message }}
rogerkeays / unchecked Public
* Notifications
* Fork 0
* Star 38
Say goodbye to checked exceptions forever.
patreon.com/jamaica440
License
MIT license
38 stars 0 forks
Star
Notifications
* Code
* Issues 0
* Pull requests 0
* Actions
* Projects 0
* Security
* Insights
More
* Code
* Issues
* Pull requests
* Actions
* Projects
* Security
* Insights
rogerkeays/unchecked
This commit does not belong to any branch on this repository, and may
belong to a fork outside of the repository.
main
Switch branches/tags
[ ]
Branches Tags
Could not load branches
Nothing to show
{{ refName }} default View all branches
Could not load tags
Nothing to show
{{ refName }} default
View all tags
Name already in use
A tag already exists with the provided branch name. Many Git commands
accept both tag and branch names, so creating this branch may cause
unexpected behavior. Are you sure you want to create this branch?
Cancel Create
1 branch 6 tags
Code
* Local
* Codespaces
*
Clone
HTTPS GitHub CLI
[https://github.com/r]
Use Git or checkout with SVN using the web URL.
[gh repo clone rogerk]
Work fast with our official CLI. Learn more about the CLI.
* Open with GitHub Desktop
* Download ZIP
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
@rogerkeays
rogerkeays comments: clarify behaviour of report()
...
f22c8cd Jul 13, 2023
comments: clarify behaviour of report()
f22c8cd
Git stats
* 141 commits
Files
Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
LICENSE
Initial commit
February 28, 2022 14:06
README.md
README: put command line usage before maven
July 13, 2023 22:11
TestAttrErrors.java
separate error testing by phase so errors don't block each other
July 10, 2023 07:45
TestFlowErrors.java
separate error testing by phase so errors don't block each other
July 10, 2023 07:45
TestWarnings.java
add test to ensure existing warnings are unaffected
July 10, 2023 14:01
Unchecked.java
comments: clarify behaviour of report()
July 13, 2023 23:41
build.sh
bump to version 0.4.2
July 13, 2023 19:30
unchecked.jar
rebuild jar: 0.4.1 build deployed to central
July 13, 2023 19:27
View code
[ ]
Unchecked Java: Say Goodbye to Checked Exceptions Forever Quick Start
Install Using Maven Install Using Gradle Build It Yourself JDK
Support IDE Support Known Issues Related Resources Disclaimer
README.md
Unchecked Java: Say Goodbye to Checked Exceptions Forever
Unchecked allows you to treat Java's checked exceptions as though
they were unchecked.
Before:
List.of("LICENSE", "README.md", "Unchecked.java").stream()
.map(file -> {
try {
return(file + ": " + Files.lines(Paths.get(file)).count());
} catch (IOException e) {
throw new RuntimeException(e); // java made me do it
}
})
.toList();
After:
List.of("LICENSE", "README.md", "Unchecked.java").stream()
.map(file -> file + ": " + Files.lines(Paths.get(file)).count())
.toList();
When you can't handle a checked exception, a common practise is to
rethrow it as a RuntimeException. The problem with this, apart from
the code pollution, is that the root cause of exceptions get hidden,
and sometimes lost if developers forget to retain it. With Unchecked,
wrapping checked exceptions is longer necessary. The exception will
just be passed back up the call stack.
Before:
public static void rm() {
try {
Files.delete(Paths.get("unchecked.kt"));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
After:
public static void rm() {
Files.delete(Paths.get("unchecked.kt"));
}
Unchecked works by converting checked exception errors to warnings.
Before:
TestValid.java:42: error: unreported exception UnsupportedEncodingException; must be caught or declared to be thrown
assert new String(STARS, "UTF-8").equals("***");
^
1 errors
BUILD FAILURE
After:
TestValid.java:42: warning: unreported exception java.io.UnsupportedEncodingException not caught or decalared to be thrown
assert new String(STARS, "UTF-8").equals("***");
^
1 warnings
BUILD SUCCESS
Unchecked does not make any changes to your bytecode. This is
possible because the JVM does not know about checked exceptions. It's
been the compiler holding you back all this time.
Before:
$ ls -o target/classes/org/apache/commons/lang3/stream/
-rw-r--r-- 1 guybrush 620 Jul 8 23:42 IntStreams.class
-rw-r--r-- 1 guybrush 254 Jul 8 23:42 'LangCollectors$1.class'
-rw-r--r-- 1 guybrush 3262 Jul 8 23:42 'LangCollectors$SimpleCollector.class'
-rw-r--r-- 1 guybrush 5223 Jul 8 23:42 LangCollectors.class
-rw-r--r-- 1 guybrush 137 Jul 8 23:42 package-info.class
-rw-r--r-- 1 guybrush 3841 Jul 8 23:42 'Streams$ArrayCollector.class'
-rw-r--r-- 1 guybrush 2036 Jul 8 23:42 'Streams$EnumerationSpliterator.class'
-rw-r--r-- 1 guybrush 5314 Jul 8 23:42 'Streams$FailableStream.class'
-rw-r--r-- 1 guybrush 6395 Jul 8 23:42 Streams.class
After:
$ ls -o target/classes/org/apache/commons/lang3/stream/
-rw-r--r-- 1 guybrush 620 Jul 8 23:49 IntStreams.class
-rw-r--r-- 1 guybrush 254 Jul 8 23:49 'LangCollectors$1.class'
-rw-r--r-- 1 guybrush 3262 Jul 8 23:49 'LangCollectors$SimpleCollector.class'
-rw-r--r-- 1 guybrush 5223 Jul 8 23:49 LangCollectors.class
-rw-r--r-- 1 guybrush 137 Jul 8 23:49 package-info.class
-rw-r--r-- 1 guybrush 3841 Jul 8 23:49 'Streams$ArrayCollector.class'
-rw-r--r-- 1 guybrush 2036 Jul 8 23:49 'Streams$EnumerationSpliterator.class'
-rw-r--r-- 1 guybrush 5314 Jul 8 23:49 'Streams$FailableStream.class'
-rw-r--r-- 1 guybrush 6395 Jul 8 23:49 Streams.class
Unchecked is invoked as a compiler plugin and has no runtime
dependencies. Warnings can be suppressed with the nowarn option.
Quick Start
Download the jar, place it on your classpath, and run javac with
-Xplugin:unchecked and -J--add-opens=java.base/java.lang=ALL-UNNAMED:
wget https://github.com/rogerkeays/unchecked/raw/main/unchecked.jar
javac -cp unchecked.jar -Xplugin:unchecked -J--add-opens=java.base/java.lang=ALL-UNNAMED Test.java
Run your code like you always have:
java Test
Or start a jshell session:
jshell --class-path unchecked.jar -C-Xplugin:unchecked -J--add-opens=java.base/java.lang=ALL-UNNAMED
Note, to suppress warnings about checked exceptions, add the nowarn
parameter using this syntax:
-Xplugin:"unchecked nowarn"
Install Using Maven
Add the following dependency to your pom.xml:
io.github.rogerkeays
unchecked
0.4.1
compile
And configure the compiler plugin:
org.apache.maven.plugins
maven-compiler-plugin
3.11.0
-Xplugin:unchecked
-J--add-opens=java.base/java.lang=ALL-UNNAMED
true
...
Note, older versions of the compiler plugin use a different syntax.
Refer to the Maven Compiler Plugin docs for more details. Make sure
you add the true option too.
To add the nowarn parameter, use this syntax (note: no quotation
marks):
-Xplugin:unchecked nowarn
Install Using Gradle
Add the following to your build.gradle:
dependencies {
compileOnly 'io.github.rogerkeays:unchecked:0.4.1'
testCompileOnly 'io.github.rogerkeays:unchecked:0.4.1'
}
tasks.withType( JavaCompile ) {
options.compilerArgs += [ '-Xplugin:unchecked' ]
options.fork = true
options.forkOptions.jvmArgs += [ '--add-opens=java.base/java.lang=ALL-UNNAMED']
}
If your build is using an annotations processor, change the
dependency tasks to:
dependencies {
annotationProcessor 'io.github.rogerkeays:unchecked:0.4.1'
testAnnotationProcessor 'io.github.rogerkeays:unchecked:0.4.1'
}
Build It Yourself
Unchecked is built using a POSIX shell script:
git clone https://github.com/rogerkeays/unchecked.git
cd unchecked
./build.sh install
JDK Support
Unchecked is tested with the following JDKs:
* jdk-09.0.4
* jdk-10.0.2
* jdk-11.0.8
* jdk-12.0.2
* jdk-13.0.2
* jdk-14.0.2
* jdk-15.0.2
* jdk-16.0.2
* jdk-17.0.2
* jdk-18.0.2.1
* jdk-19.0.2
* jdk-20.0.1
* jdk-21 (early access)
* jdk-22 (early access)
To ensure backwards compatibility with existing code, Unchecked has
been used to compile and test the following open source projects:
* Apache Commons Lang
* iText
* Tomcat
* Hibernate
Unchecked should play nice with other javac plugins. It works with
Lombok and Fluent, at least.
IDE Support
There is currently no IDE support for Unchecked. Contributions are
welcome. If you cannot contribute code, please consider supporting us
on Patreon.
Known Issues
* If you are using Unchecked with Fluent, we recommend you specify
the -Xplugin:unchecked option first, as this is how it is tested.
You will also need at least JDK 11 or newer.
* Setting shell variables like OPTS=-Xplugin:"unchecked nowarn"
cause string expansion hell because of the quotes. It works if
you quote "$OPTS", but gets pretty nasty when you want to add
more OPTS. The trick is to use two different variables: javac
"$PLUGIN" "$OPTS".
Please submit issues to the github issue tracker. Be sure to include
the JDK version and build tools you are using. A snippet of the code
causing the problem will help to reproduce the bug. Before
submitting, please try a clean build of your project.
Related Resources
* Jamaica: our project's Patreon page. If you use Unchecked, please
support us .
* Fluent: static extension methods for Java (also part of Jamaica).
* Kotlin: a JVM language which supports extension methods out of
the box.
* Lombok: the grand-daddy of javac hacks, with various tools for
handling checked exceptions.
* Manifold: a javac plugin with many features, including disabling
checked exceptions.
* Checked exception helper functions: band-aids for when you can't
use Unchecked .
* More solutions looking for a problem.
Disclaimer
* Unchecked is not supported or endorsed by the OpenJDK team.
* The reasonable man adapts himself to the world. The unreasonable
one persists in trying to adapt the world to himself. Therefore
all progress depends on the unreasonable man. --George Bernard
Shaw
About
Say goodbye to checked exceptions forever.
patreon.com/Jamaica440
Topics
java programming programming-languages exception-handling javac
checked-exceptions
Resources
Readme
License
MIT license
Stars
38 stars
Watchers
4 watching
Forks
0 forks
Report repository
Languages
* Java 59.0%
* Shell 41.0%
Footer
(c) 2023 GitHub, Inc.
Footer navigation
* Terms
* Privacy
* Security
* Status
* Docs
* Contact GitHub
* Pricing
* API
* Training
* Blog
* About
You can't perform that action at this time.