Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active April 2, 2023 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/a77e2c78fdff495e34b9a4d1aad36544 to your computer and use it in GitHub Desktop.
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/c0e0032b89b056415143542f3356b94c139c2c8c
// summary : java feature - checked versus unchecked exceptions
// keywords : java, scalacli, checked, unchecked, exception, @testable
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// 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