Created
February 26, 2018 22:54
-
-
Save max-l/5265e68c17453884772f7219504ccb28 to your computer and use it in GitHub Desktop.
TestyDigitalMode created by max_l - https://repl.it/@max_l/TestyDigitalMode
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
| class EndOfTheWorldException extends Exception { | |
| public EndOfTheWorldException(String reason) { | |
| super("The world has ended, because of "+ reason); | |
| } | |
| } | |
| class Main { | |
| public static void main(String[] args) { | |
| System.out.println("Hello world!"); | |
| } | |
| /* | |
| this method does not compile, because it throws a checked exception that is neither | |
| declared (throws clause) or caught, if uncommented, the compiler will output this error : | |
| Main.java:20: error: unreported exception EndOfTheWorldException; must be caught or declared to be thrown | |
| throw new EndOfTheWorldException("it happened !"); | |
| ^ | |
| 1 error | |
| / public static void dangerousMethod1() { | |
| // throw new EndOfTheWorldException("it happened !"); | |
| // } | |
| */ | |
| // this method correctly declares the throwing of EndOfTheWorldException in it's signature : | |
| public static void dangerousMethod2() throws EndOfTheWorldException { | |
| throw new EndOfTheWorldException("it happened !"); | |
| } | |
| public static void dangerousMethodThatCanRecoverFromDisasters() { | |
| try { | |
| throw new EndOfTheWorldException("yes... it happened !"); | |
| } | |
| catch(EndOfTheWorldException e) { | |
| // recover from end of the world... | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment