multiple statements in try/catch block - Java - java

Im a bit uncertain as to whether this:
try{
worldHeight = Integer.parseInt(JOptionPane.showInputDialog("How many cells high will the world be?: "));
}
catch (NumberFormatException e){
JOptionPane.showMessageDialog(null, "You have not entered a valid number");
}
try{
worldWidth = Integer.parseInt(JOptionPane.showInputDialog("How many cells wide will the world be?: "));
}
catch (NumberFormatException e){
JOptionPane.showMessageDialog(null, "You have not entered a valid number");
}
would do the same thing as:
try{
worldHeight = Integer.parseInt(JOptionPane.showInputDialog("How many cells high will the world be?: "));
worldWidth = Integer.parseInt(JOptionPane.showInputDialog("How many cells wide will the world be?: "));
}
catch (NumberFormatException e){
JOptionPane.showMessageDialog(null, "You have not entered a valid number");
}
basically want the user to enter a number, if it isnt a number exception gets thrown and the user gets re-asked for a number?
Thanks

In your first example, with the two separate try...catch blocks, it seems that when an exception is thrown, you are just showing a dialog, not stopping the flow of control.
As a result, if there is an exception in the first try...catch, control will continue to the second try...catch and the user will be asked to enter the second number, regardless of the fact that she did not enter the first number correctly.
In the second example, if there is an exception in the first try...catch, the user will not be presented with the second question, because control will not continue inside the try block, but rather after the catch block's end.

Yes this will work (almost) the same. In a try catch block it will only stop execution at the point where the error occurs. If it throws an error at the first line the second line will never be executed in the second option. That is the only difference, in the first option, the second line (input) will be executed no matter if the first line (input) throws an error or not.

You can also try following code
{
int arr[]=new int[5];
try
{
try
{
System.out.println("Divide 1");
int b=23/0;
}
catch(ArithmeticException e)
{
System.out.println(e);
}
try
{
arr[7]=10;
int c=22/0;
System.out.println("Divide 2 : "+c);
}
catch(ArithmeticException e)
{
System.out.println("Err:Divide by 0");
}
catch(ArrayIndexOutOfBoundsException e)
//ignored
{
System.out.println("Err:Array out of bound");
}
}
catch(Exception e)
{
System.out.println("Handled");
}
}
For more detail visit: Multiple try-catch in java with example

Related

How to fix the Stackoverflow in the following method?

I am trying to program a method that handles user Input. The method needs to scan from the console an int, check if scanned int was in Range and then check the validity of the data before scanning another int in another method. I decided to program the method recursively, that it will call itself to repeat if the mentioned conditions are not met.
public static void readUserInputDay(Scanner scanner) {
System.out.print("Day (1-31): ");
try {
int tmp = scanner.nextInt();
day = new Integer(tmp);
if(isTheInputInRange(day.intValue(), DAY)) {
readUserInputMonth(scanner);
} else {
System.out.print("Number isn't in Range (1-31)\n");
readUserInputDay(scanner);
}
} catch (Exception e) {
System.out.print("Please enter a number!\n");
readUserInputDay(scanner);
}
}
The other filters work as expected, however if I enter on the console something that is not an int the Exception is triggered and catched (As expected) but when I expect the Method to recursively repeat itself, I instead get the following output on the console:
Day (1-31): Please enter a number!
Day (1-31): Please enter a number!
Day (1-31): Please enter a number!
Exception in thread "main" java.lang.StackOverflowError
at sun.nio.cs.UTF_8.updatePositions(UTF_8.java:77)
at sun.nio.cs.UTF_8.access$200(UTF_8.java:57)
at sun.nio.cs.UTF_8$Encoder.encodeArrayLoop(UTF_8.java:636)
at sun.nio.cs.UTF_8$Encoder.encodeLoop(UTF_8.java:691)
at java.nio.charset.CharsetEncoder.encode(CharsetEncoder.java:579)
at sun.nio.cs.StreamEncoder.implWrite(StreamEncoder.java:271)
at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:125)
at java.io.OutputStreamWriter.write(OutputStreamWriter.java:207)
at java.io.BufferedWriter.flushBuffer(BufferedWriter.java:129)
at java.io.PrintStream.write(PrintStream.java:526)
at java.io.PrintStream.print(PrintStream.java:669)
at MyClass.readUserInputDay(MyClass.java:27)
at MyClass.readUserInputDay(MyClass.java:43)
at MyClass.readUserInputDay(MyClass.java:43)
Do have any ideas how I need to fix the code, so when method call itself, it doesn't enter immediately in the catch block ?
Thanks in advance
you are calling the method inside of itself 3 times and according to conditions it cause to re-call it self and at the end overflow error.
to prevent from this problem at first try to change the structure of your code and use while loops for example to continue your code at a certain condition you want and get the result:
public static void readUserInputDay(Scanner scanner) {
try {
boolean isFinished = false;
// your condition for loop
while (!isFinished) {
System.out.print("Day (1-31): ");
int tmp = scanner.nextInt();
day = new Integer(tmp);
if (isTheInputInRange(day.intValue(), DAY)) {
readUserInputMonth(scanner);
isFinished = true;
} else {
System.out.print("Number isn't in Range (1-31)\n");
}
}
} catch (Exception e) {
System.out.print("Please enter a number!\n");
readUserInputDay(scanner);
}
}
It is odd that you say the StackOverflow error occurs on the first retry, especially within the first System.out.print call.
However, as Mustafa suggested, using a while loop rather than recursion is a much better choice in this case, as it will not cause new stack frames to be created every time somebody enters the wrong text (as I do not think that Java can do tail call optimisation on that method).
public static void readUserInputDay(Scanner scanner) {
while (true) {
System.out.print("Day (1-31): ");
try {
int tmp = scanner.nextInt();
day = new Integer(tmp);
if (isTheInputInRange(day.intValue(), DAY)) {
readUserInputMonth(scanner);
break; // exit the retry loop
} else {
System.out.print("Number isn't in Range (1-31)\n");
}
} catch (Exception e) {
System.out.print("Please enter a number!\n");
}
// By this point, the input is invalid, so loop again
}
}

Python retry equivalent in Java

I am very new to java and I am trying out error handling. I am pretty proficent in python and I know the error handling in python would go
while True:
try:
*some code*
except IndexError:
continue
break
I would like to know what the equivalent of a retry loop after exception is in java
EDIT:
This is what I have so far, however whenever a exception is thrown it does an infinite loop saying "Enter an Short: Error Try again."
while(true)
{
try {
System.out.print("Enter an Short: "); //SHORT
short myShort = reader.nextShort();
System.out.println(myShort);
break;
}
catch (InputMismatchException e) {
System.out.println("Error Try again.");
continue;
}
}
To clarify what exactly I would like is. When "InputMismatchException" is thrown the loop re runs and prompts the user again and it does this until the user gives the correct input. I hope that clarifies what I would like it to do.
What you have is almost good as #Thomas mentioned. Just need to add some brackets and semicolons. It should look line following code.
while(true){
try{
// some code
break; // Prevent infinite loop, success should break from the loop
} catch(Exception e) { // This would catch all exception, you can narrow it down ArrayIndexOutOfBoundsException
continue;
}
}
As your question asks about error handling and you showed IndexError as an example, the equivalent in Java could be:
try {
//*some code*
}
catch(ArrayIndexOutOfBoundsException exception) {
//handleYourExceptionHere(exception);
}
About ArrayIndexOutOfBoundsException, you take a look here, in the documentation. About Exceptions, in general, you can read here.
EDIT, according to your question edition, adding more information...
while(true)
{
try {
System.out.print("Enter a short: ");
short myShort = reader.nextShort();
System.out.println(myShort);
}
catch (InputMismatchException e) {
System.out.println("Error! Try again.");
//Handle the exception here...
break;
}
}
In this case, when the InputMismatchException occurs, the error message is exhibited and the break should leave the loop. I do not know yet if I understand well what you are asking, but I hope this helps.
To the help of #Slaw he determined that scanner would keep inputting the same value unless I closed it at the end of the loop and here is the working code.
while (true)
{
Scanner reader = new Scanner(System.in);
try
{
System.out.print("Enter an Short: "); //SHORT
short myShort = reader.nextShort();
System.out.println(myShort);
reader.close();
break;
}
catch (InputMismatchException e)
{
System.out.println("Error Try again.");
}
}

Im trying to add a try catch that tells the user they cant plug in negative numbers

I was able to add a try catch that tells the user that they cant use
letters.However for some reason adding a try catch for negative numbers dosent seem to work.I know that the try block is where if somthing can go wrong like entering in a negative number the catch can print out the error message. I think thats where my problem lies. Another problem that is associated with the try catch is that I'm use to the user entering in -1 to enter the contents that the user inputs so I'm thinking its gonna cause a logical problem.
tl;dr Adding a try catch or another catch to prevent user from adding negative numbers
this is not the the whole program but what it does is that it filters out the integers that the user inputs and separates the evens and odds.
public static void main(String [] args)
{
Scanner stdin = new Scanner(System.in);//for user input
int[] evenNum = new int [100];//Even Array up too 100
int[] oddNum = new int[100];//Odd Array up too 100
int evenIndex=0;//even numbers
int input=0;//user input
int i=0;//incrementer for arrays
int k=0;
int j=0;
String name;
System.out.println("Type In Your Name");//Type in name
name = stdin.nextLine();
while ((i < oddNum.length && i < evenNum.length) && input !=-1)//100 numbers only
{
try{//this is what we want anything else the catch will block it and display a message
System.out.println(name+" Enter a positive number, Enter -1 For results");
input= stdin.nextInt();
oddNum[i]=input;//holds input
i++;//Increments array
}
catch(Exception d){
System.out.println("Only Positive Numbers & no Letters Please!");
stdin.next();
}
}
You can check the input variable after you get it from the scanner
if (input < 0) {
System.out.println("Only Positive Numbers & no Letters Please!");
}
Your code does not throw any Exception when the number is read from the scanner. So you cannot expect that the execution jumps into the catch-block when you enter a negative number.
But you can alternatively throw an exception when the input is negative. This will make the thread to jump directly into the catch-block. In the catch-block you can then print the message you passed the IllegalArgumentException
if (input < 0) {
// this gets caught in the catch block
throw new IllegalArgumentException("Only Positive Numbers & no Letters Please!");
}
...
} catch (IllegarArgumentException e) {
System.out.println(e.getMessage());
}
It is generally bad practice to catch Exception (java.lang.Exception). This is the "root" of all checked exceptions and the catch-block will be jumped into whenever any subclass of Exception is thrown.
Just catch the concrete exception that you are expecting. (In this case IllegalArgumentException.)
Also you should not use exceptions to control the execution flow of your program.
I would suggest something like this:
do {
System.out.println(name+" Enter a positive number, Enter -1 For results");
try {
input = stdin.nextInt();
} catch (java.util.InputMismatchException e) { // if the user enters something that is not an integer
System.out.println("Please only enter integers");
input = Integer.MIN_VALUE;
stdin.next(); // consume the non-int so we don't get caught in an endless loop
}
} while (input < -1); // loop as long as the input is less than -1
if (input == -1) {
// show the results here
}
This will accept positive integers and will prompt for an input until the user enters a positive number, 0 (zero) or -1 (which should show the results)
You can do it like this:
if (input < 0) {
throw new IllegalArgumentException();
}
Now if the number is negative, it will throw exception and the catch code can be executed. Because you catch Exception so all of the exception will be catches here.
Note: In catch block you no need to add stdin.next(); because the program will continue from the first line of while loop.
In order for your catch block to catch exception, the Exception needs to be thrown from the code. In case of negative numbers the line input= stdin.nextInt(); will not throw exception as it is perfectly legal for integer to be negative. You will need to add if condition like this:
input = stdin.nextInt();
if ( input < 0 ) {
throw new Exception("Negative number entered");
}
But some consider this to be bad practice because you are using exceptions to control the flow of a program. So I give you another example how you can do this without throwing an exception:
input = stdin.nextInt();
if ( input < 0 ) {
System.out.println("Only Positive Numbers Please");
continue; // will continue from the beginning of a loop
}

Java try-catch infinite loop when input a character

I want to make a try-catch exception that only accept 1-5 or 9 input. So I wrote the following code.
try {
step1 = scanner.nextInt();
} catch (Exception ex) {
System.out.println("Error! Input accept integers only. Input 1-5 or 9");
System.out.println("");
continue;
}
The result is that if I input an invalid number, it gave me an error (That's true). But when I input a character, it gave me an infinite loop. How can I solve the problem?
Since i don´t know how your loop looks i´ll just go with an endless loop in my answer. In the normal case, the nextInt method wont catch the carriage return, and if you input something that is not a number you need to call nextLine to catch this. If you don´t do this you might run into an infinity loop if you are using any kind of loop that just asks for nextInt. This could solve the problem:
while(true) {
int step1;
try {
step1 = s.nextInt();
} catch (InputMismatchException e) {
System.out.println("Error! Input accept integers only. Input 1-5 or 9");
System.out.println("");
} finally { // Use a finally block to catch the carriage return, no matter if the int that got input was valid or not.
s.nextLine();
}
}
Maybe you must parse input before decision making.
Sorry for my bad English, I'm not a native speaker also.
try this, which make all the tests
int step1;
while (true)
{
Scanner scanner = new Scanner(System.in);
try {
step1 = scanner.nextInt();
if (((step1>=1) && (step1<=5)) || (step1==9))
// BINGO
break;
}
catch (Exception ex)
{
}
System.out.println("Error! Input accept integers only. Input 1-5 or 9");
System.out.println("");
// it loops ther
}
System.out.println("You are done ! "+step1);

Java prints repeatedly if try catch is used

I am making a basic application where it trains your math skills. I have this code:
while (true)
{
try
{
int userAnswer;
System.out.println("Type quit to exit to the menu!");
int randInt = r.nextInt(num2);
System.out.println(num1 + " + " + randInt + " =");
userAnswer = in.nextInt();
if(userAnswer == num1 + randInt) System.out.println("Correct!");
else System.out.println("Wrong!");
break;
}
catch(Exception e)
{
}
}
When someone prints out a d or something in the answer, the try catch goes. But, then it goes to the while loop and repeatedly spams Type quit to exit to the menu and then something like 1 + 2 = infinitely... I think I know what's wrong, userAnswer has been assigned already as something that throws an exception that goes to the catch and it just keeps printing those and goes to the catch and goes back because userAnswer is already assigned. I think this is what is happening, I could be wrong. Please help!
EDIT: I forgot to make this clear, but I want the question to be re-printed again, exiting out of the loop goes to a menu where you can't get the question back, I want it to redo what's in the try catch...
You should never catch an Exception without handling it.
catch(Exception e)
{
System.out.println("An error has occured");
break;
}
This should stop your program from looping infinitely if an Exception occurs.
If user input comes as letter it will get an exception because you are trying to read(parse) as integer. So your catch clause is in the loop you have to write break in there to go out from loop.
Still i will suggest you to getline as string and than compare with your cli commands (quit in your case) than you can try to parse it as an integer and handle loop logic.
You're not breaking the while loop if there is a mismatch
while(true)
{
try
{
}
catch(InputMisMatchException e)//I suggest you to use the exact exception to avoid others being ignored
{
System.out.println("Thank you!");
break;//breaks the while loop
}
}
Yoy're not breaking the loop in case of Exception occurs.
Add break; statement in the catch block to run your program without going to infinite loop, in case exception occurs.
Since the given answers don't match your requirement I'll solve that "riddle" for you.
I guess what you didn't knew is that the scanner won't read the next token if it doesn't match the expectation. So, if you call in.nextInt() and the next token is not a number, then the scanner will throw an InputMismatchException and keeps the reader position where it is. So if you try it again (due to the loop), then it will throw this exception again. To avoid this you have to consume the erroneous token:
catch (Exception e) {
// exception handling
in.next();
}
This will consume the bad token, so in.nextInt() can accept a new token. Also there is no need to add break here.
Mind that in.next() reads only one token, which is delimited by a whitespace. So if the user enters a b c, then your code will throw three exception and therefore generate three different question befor the user can enter a number. You can avoid that by using in.nextLine() instead. But this can lead into another problem: Scanner issue when using nextLine after nextXXX, so pay attention to that :).

Categories