Skip to content

Instantly share code, notes, and snippets.

@AlinaWithAFace
Created April 18, 2018 16:25
Show Gist options
  • Save AlinaWithAFace/326b093bbb64cc7d784f32e1b81650e8 to your computer and use it in GitHub Desktop.
Save AlinaWithAFace/326b093bbb64cc7d784f32e1b81650e8 to your computer and use it in GitHub Desktop.
An example micro-program with an example exception and exception handling
//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