Java Is Making User Hit Enter Before Prompting Again After Illegal Argument Exception Is Thrown
Whenever you use a statement that might throw an exception in Java, you should write special code to anticipate and catch the exception. That way, your program won't crash if the exception occurs.
Y'all take hold of an exception by using a try argument, which has this general class:
try { statements that can throw exceptions } take hold of (exception-blazon identifier) { statements executed when exception is thrown } Here, you place the statements that might throw an exception within a try cake. And so you catch the exception with a catch block.
Hither are a few things to annotation about try statements:
-
You tin can code more than than ane catch block. That fashion, if the statements in the try block might throw more than than one blazon of exception, you tin grab each type of exception in a separate catch block.
-
For scoping purposes, the try block is its ain self-contained block, separate from the catch block. Equally a consequence, any variables you declare in the endeavour cake are not visible to the catch block. If you want them to be, declare them immediately before the try statement.
-
You tin can also lawmaking a special cake (called a finally cake) after all the catch blocks. For more than information virtually coding finally blocks.
-
The various exception classes in the Java API are divers in different packages. If yous utilize an exception class that isn't divers in the standard java.lang packet that's always available, you demand to provide an import argument for the bundle that defines the exception class.
A simple example
To illustrate how to provide for an exception, here'south a program that divides two numbers and uses a try/catch argument to catch an exception if the 2d number turns out to be nothing:
public class DivideByZero { public static void chief(String[] args) { int a = five; int b = 0; // you know this won't work effort { int c = a / b; // but you endeavour it anyway } catch (ArithmeticException e) { System.out.println("Oops, you can't " + "split by zero."); } } } Here, the division occurs within a try block, and a catch block handles ArithmeticException. ArithmethicException is divers past java.lang, so an import statement for it isn't necessary.
When you lot run this programme, the following is displayed on the panel:
Oops, you can't divide past zero.
In that location'south naught else to run across here.
Some other instance
Here is a simple example of a program that uses a method to go a valid integer from the user. If the user enters a value that isn't a valid integer, the grab block catches the error and forces the loop to echo.
import java.util.*; public class GetInteger { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { System.out.print("Enter an integer: "); int i = GetAnInteger(); Organisation.out.println("You entered " + i); } public static int GetAnInteger() { while (true) { try { return sc.nextInt(); } catch (InputMismatchException e) { sc.side by side(); Organization.out.print("That'southward not " + "an integer. Try over again: "); } } } } Here the statement that gets the input from the user and returns information technology to the methods called is coded inside the endeavour block. If the user enters a valid integer, this statement is the only one in this method that gets executed.
If the user enters data that tin can't be converted to an integer, all the same, the nextInt method throws an InputMismatchException. Then this exception is intercepted by the catch block — which disposes of the user'south wrong input past calling the adjacent method, too equally past displaying an error message. Then the while loop repeats.
Here's what the console might look like for a typical execution of this program:
Enter an integer: 3 That's not an integer. Try again: 3.001 That'southward non an integer. Endeavor again: three You entered iii
Hither are a couple other things to annotation most this plan:
-
The import statement specifies coffee.util.* to import all the classes from the java.util parcel. That way, the InputMismatchException class is imported.
-
The next method must exist called in the catch block to dispose of the user's invalid input because the nextInt method leaves the input value in the Scanner's input stream if an InputMismatchException is thrown. If yous omit the statement that calls next, the while loop keeps reading it, throws an exception, and displays an fault message in an infinite loop.
This fault was found the hard mode. (The merely way to arrive stop is to close the console window.)
About This Article
This article is from the volume:
- Java All-in-One For Dummies ,
This article can be found in the category:
- Java ,
greshamwitheniand.blogspot.com
Source: https://www.dummies.com/article/technology/programming-web-design/java/how-to-catch-exceptions-in-java-153217/
0 Response to "Java Is Making User Hit Enter Before Prompting Again After Illegal Argument Exception Is Thrown"
Post a Comment