Created
September 5, 2016 01:51
-
-
Save gg-spyda/a8ceb37e709863339c74b30bdf0e3ce4 to your computer and use it in GitHub Desktop.
Exceptions in Java
This file contains 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
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