Question

I'm having a little difficulty with a catch block in java. Help would be very much appreciated.

do {
    System.out.println ("If you want to exit the program, press 0. To continue, press 1.");
        try {
            returnint = Input.nextInt();    
        } catch (Exception e) {
            returnint=12;
            return; //after this code executes, I CANNOT RE-ENTER A NUMBER
        } finally {
        if (!(returnint==1 || returnint==0)) {
            System.out.println ("Invalid response. Please retry.");
        } continue;
    } while (returnint!=1 && returnint!=0);

So the problem is for some reason, the loop won't repeat. Any help with this, including a better understanding of try-catch-finally blocks, would be very much appreciated.

EDIT: There is also a try block at the start of the program, and after the program returns there, it somehow triggers this as well. Any help here?

EDIT 2: Full code below.

import java.util.Scanner;


public class PrimeNumberChecker {

/**
 * @param args
 */
public static void main(String[] args) {
    int returnint=1;
    boolean isprime=true;
    Scanner Input = new Scanner(System.in); //creates scanner
do{ 
    long prime,root,primediv,i;
    try {
    System.out.println("Please input a number to be checked"); //prompts user for input
    prime = Input.nextLong();//detects and stores next int input
    }catch(Exception exc){
        System.out.println("The following exception has been thrown: "+exc+"  Program aborted.");
        returnint=0;
        return;
    }
    root=(long) Math.sqrt(prime); //takes the int sqrt of the input number
    for(i=2;(i<=root&&isprime==true);i++){ //for loop to check for prime factors
        if (prime%i==0) {  //if a number divides into the number exactly, this returns true
            isprime=false;  //and so this returns false
        }
    }

    if (isprime==false) {
        primediv=prime;
        System.out.println("The number is not prime.");
        System.out.println("The factors of the number are: ");
            for(i=2;primediv>1;i++) {   //this is a loop to factorise the number if it is NOT prime
                if (primediv%i==0) {   //if it finds a factor, it prints it and checks if the factor appears twice
                    primediv=primediv/i;
                    System.out.println(i);
                    i--;
                }
            }
    } else {
        System.out.print(prime);  //the output if the number is prime
        System.out.println(" is a prime number");
    }
    try {
        do {
            System.out.println ("If you want to exit the program, press 0. To continue, press 1.");
            if (!(returnint==1 || returnint==0)) {
                System.out.println ("Invalid response. Please retry.");
            }
            returnint = Input.nextInt();    
          continue;
        } while (returnint!=1 && returnint!=0);
    } catch (Exception e) {
        System.out.println("ERROR: " + e.getMessage());
        throw e;
    } finally {   
        System.out.println("Exit. Return value was set to " + returnint);
    }   
}while (returnint==1);
}

}

Was it helpful?

Solution

How about this:

int returnint = 0;
try {
    do {
        System.out.println ("If you want to exit the program, press 0. To continue, press 1.");
        if (!(returnint==1 || returnint==0)) {
            System.out.println ("Invalid response. Please retry.");
        }
        returnint = Input.nextInt();    
      continue;
    } while (returnint!=1 && returnint!=0);
} catch (Exception e) {
    System.out.println("ERROR: " + e.Message());
} finally {   
    System.out.println("Exit. Return value was set to " + returnint);
}   

OTHER TIPS

return statement returns the control back to the calling program. Have you tried commenting it out and see the execution.

First decide on the logic on what you want to do when you catch an exception.

You want to pull the return out of your catch block. It should read:

}catch(Exception e){
    returnint=12;
}finally{

With the return statement, your code returns to the calling method and skips the rest of the method. Your finally block will still execute, by virtue of how a try/catch/finally block operates, but none of the rest of your code will execute.

why are u setting the returnint to 12 in catch block. Once you return from a method you can not enter the loop. Catch blocks are supposed to catch exceptions and display meaningful messages to user or throw the exception to the calling class. In your example you have eaten up the exception and returned as if the method executed normally

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top