3

Below is my code

public class ExceptionHandling {

    public static void main(String[] args) throws InputMismatchException{
        Scanner sc = new Scanner(System.in);
        int a = 0;
        int b = 0;
        try {
            a = sc.nextInt();
            b = sc.nextInt();

            try {
                int c = a / b;
                System.out.println(b);

            } catch (ArithmeticException e) {
                System.out.println(e);
            }
        } catch (InputMismatchException e) {
            System.out.println(e);
        }

    }

}

My main query from the above question is, when I am passing String as input then I am getting java.util.InputMismatchException only. But when I am passing 2147483648 as an input, it gives java.util.InputMismatchException: For input string: "2147483648" as an output.

So can anyone tell me why I am getting For input string: "2147483648" in that case?

4 Answers 4

2

My main issue is, while passing "hello" the output is java.util.InputMismatchException. But while passing (2147483648) long in int type the output is= java.util.InputMismatchException: For input string: "2147483648". I want to know why is it printing extra content.

That is a different question to what you were originally asking, but I will answer anyway.

The reason you are getting "extra content" as in:

java.util.InputMismatchException: For input string: "2147483648"

is that you are printing the exception like this:

System.out.println(e);

This calls toString() on the exception object and prints it. The toString() method for a typical exception is roughly equivalent to this:

public String toString() {
    return e.getClass().getName() + ": " + e.getMessage();
}

If you don't want the exception name, just print the exception message:

System.out.println(e.getMessage());

If you what the exception name without the message:

System.out.println(e.getClass().getName());

And so on.

(IMO, none of these are the kind of message you should be showing to users. They don't explain the problem in terms that a user would understand!)


I want the output to be same for both Hello and 2147483648.

It will be, I think. For "Hello", the output will be:

java.util.InputMismatchException: For input string: "Hello"

Finally, if you actually want an intelligible error message, you will need to make more extensive modifications to the code. Unfortunately, neither nextInt() or Integer.parseInt(...) give exception messages that explains:

  • that an int is expected, and
  • why the input string is not an acceptable int value.

Addressing this in a general way is difficult. The Scanner.nextInt method doesn't "understand" what the number means in the context of the call. In the particular case, you could call sc.next() to get the string, and manually test if it is an out-of-range integer or something else. But be aware that the Scanner.nextInt uses locale-sensitive parsing.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks man your answer was helpful, by using System.out.println(e.getClass().getName()); it helped me
1

The value 2147483648 is larger than the maximum value which can fit into a primitive Java integer, which is 2147483647. Java integers only fit any value between -2147483648 and 2147483647 [-231 to 231-1, since java int is a 32-bit integer]. To get around this problem, either use an input within range for integers, or maybe use a wider type, such as long:

long a = 0;
long b = 0;

try {
    a = sc.nextLong();
    b = sc.nextLong();
    // ...
}
catch (Exception e) { }

3 Comments

Yeah I understand what you are saying, it is out of range for int, i get that. But my main issue is, while passing "hello" the output is java.util.InputMismatchException. But while passing (2147483648) long in int type the output is= java.util.InputMismatchException: For input string: “2147483648”. I want to know why is it printing extra content. I want the output to be same for both Hello and 2147483648.
Well passing an out of range value to an integer and passing a string are both triggering the same exception. Why do you need anything finer grain than this?
It's a programming question on Hackerrank,the output should be same for both string and 2147483648. The exception is same in both cases, but for string it is=java.util.InputMismatchException & for 2147483648 it is =java.util.InputMismatchException: For input string: “2147483648”..I dont know why it is printing (For input string: “2147483648”).
1

The exception message includes the string value itself to give more insight about the mistake because the "2147483648" string is a string that resembles an integer but cannot be represented as one because of its size. The string value for "hello," on the other hand, is obviously not an integer, thus it is unnecessary to include it in the exception message.

Comments

-1

dont print exception message just print the exception you in the stdout section.

catch (ArithmeticException e) {
            System.out.println("java.lang.ArithmeticException: / by zero");
        } catch (InputMismatchException e) {
            System.out.println("java.util.InputMismatchException");
        }

just like that

2 Comments

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
Sorry, but this adds nothing that hasn't already been explained (more clearly) in earlier answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.