Skip to content

Instantly share code, notes, and snippets.

@Jivvie

Jivvie/Main.java

Created Sep 5, 2016
Embed
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(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
You can’t perform that action at this time.