Skip to content

Instantly share code, notes, and snippets.

@gg-spyda
Created September 5, 2016 01:51
Show Gist options
  • Save gg-spyda/a8ceb37e709863339c74b30bdf0e3ce4 to your computer and use it in GitHub Desktop.
Save gg-spyda/a8ceb37e709863339c74b30bdf0e3ce4 to your computer and use it in GitHub Desktop.
Exceptions in Java
import java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int myAge;
Scanner scanner = new Scanner(System.in);
//try statement. Make sure myAge is indeed, an integer
try{
System.out.println("Enter your age");
//try this
myAge = scanner.nextInt();
}
//try much be followed by catch. it takes an "e" as a variable of error type. can be anything.
catch(InputMismatchException e)
{
System.out.println("This is your error:");
System.out.println(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment