Created
April 18, 2018 16:25
-
-
Save AlinaWithAFace/326b093bbb64cc7d784f32e1b81650e8 to your computer and use it in GitHub Desktop.
An example micro-program with an example exception and exception handling
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
//Write minimal code. You may not need all lines. | |
//A FriedEggException is a kind of Exception. | |
//Define a minimal FriedEggException to be used below. | |
class FriedEggException extends Exception { | |
public FriedEggException(String message) { | |
super(message); | |
} | |
} | |
class TryCatch { | |
//Write a very short method that will always raise a FriedEggException. | |
static void tryAThing() throws FriedEggException { | |
throw new FriedEggException("The egg broke"); | |
} | |
public static void main(String[] args) { | |
//Call the method you defined and write code to handle the exception by printing it (or the stack). | |
try { | |
TryCatch.tryAThing(); | |
} catch (FriedEggException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment