Last active
February 3, 2026 20:25
-
-
Save dacr/a77e2c78fdff495e34b9a4d1aad36544 to your computer and use it in GitHub Desktop.
java feature - checked versus unchecked exceptions / published by https://github.com/dacr/code-examples-manager #9cf3f47c-c240-4d2d-b5f6-8ab29b89c915/fdbcf0f54b4f543955be74399713e2fe8bab2146
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // summary : java feature - checked versus unchecked exceptions | |
| // keywords : java, scalacli, checked, unchecked, exception, @testable | |
| // publish : gist | |
| // authors : David Crosson | |
| // license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt) | |
| // id : 9cf3f47c-c240-4d2d-b5f6-8ab29b89c915 | |
| // created-on : 2022-01-22T10:06:48+01:00 | |
| // managed-by : https://github.com/dacr/code-examples-manager | |
| // run-with : scala-cli $file | |
| public class JavaFeatureExceptionCheckedUnchecked { | |
| // Any instances of java.lang.RuntimeException or java.lang.Error are unckecked exceptions | |
| // SO NO need to declare them in throws clauses | |
| static class MyUncheckedException1 extends java.lang.RuntimeException { | |
| public MyUncheckedException1(String message) { | |
| super(message); | |
| } | |
| } | |
| static class MyUncheckedException2 extends java.lang.Error { | |
| public MyUncheckedException2(String message) { | |
| super(message); | |
| } | |
| } | |
| static class MyCheckedException extends java.lang.Exception { | |
| public MyCheckedException(String message) { | |
| super(message); | |
| } | |
| } | |
| public static void println(String message) throws MyCheckedException { | |
| if (message == null) throw new MyUncheckedException1("is null"); | |
| if (message.length() == 0) throw new MyUncheckedException2("is empty"); | |
| if (message.equals("boum")) throw new MyCheckedException("bad message"); | |
| System.out.println(message); | |
| } | |
| public static void main(String[] args) throws MyCheckedException { | |
| var message = "hello world"; | |
| println(message); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment