Skip to content

Instantly share code, notes, and snippets.



Created Sep 5, 2016
What would you like to do?
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(;
//try statement. Make sure myAge is indeed, an integer
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:");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.