In Java, what exception type is thrown by the statement Integer.parseInt("two") and should be used in the catch block to handle this error?

Difficulty: Easy

Correct Answer: NumberFormatException

Explanation:


Introduction / Context:
Parsing strings into numbers is a common task in Java, and the wrapper classes such as Integer provide parse methods to perform this conversion. When the input string does not represent a valid number, the parse method does not return silently; it throws a runtime exception. Interview questions often use Integer.parseInt("two") as a simple example to test whether you know which exception to catch or expect when parsing fails because of an invalid numeric format.


Given Data / Assumptions:

  • The code fragment is Integer.parseInt("two").
  • The string "two" is not a valid decimal integer representation.
  • We are concerned with the specific exception type thrown by this method.
  • No custom exception handling is added around the method call in the example.


Concept / Approach:
The method Integer.parseInt(String s) attempts to interpret the string s as a signed decimal integer by default. If every character in the string is a digit (with optional leading sign) and the value fits within the int range, parsing succeeds. If the string contains non digit characters or the value is out of range, the method throws a NumberFormatException. This is an unchecked exception that extends IllegalArgumentException and signals that the input string has an inappropriate format for numeric conversion. It is not a ClassCastException, IllegalStateException, or IOException, which represent entirely different error conditions in Java.


Step-by-Step Solution:
Step 1: Examine the string literal "two". It contains alphabetic characters t, w, and o, not digits. Step 2: Recognise that Integer.parseInt expects a sequence of decimal digits, optionally preceded by a plus or minus sign, when no radix is specified. Step 3: Because "two" does not match this pattern, the parseInt method cannot return a valid integer value. Step 4: According to the Java API documentation, when the string cannot be parsed as an integer, parseInt throws NumberFormatException. Step 5: Therefore, to handle this error in a try catch block, you should catch NumberFormatException.


Verification / Alternative check:
You can verify this by writing a small Java program with a try catch block around Integer.parseInt("two") and printing the caught exception's class name. The output will show java.lang.NumberFormatException, confirming the documented behaviour. If you change the string to a valid numeric form, such as "2" or "-15", the method returns an int and no exception is thrown. This contrast demonstrates that NumberFormatException is specifically used for invalid numeric formats in string parsing.


Why Other Options Are Wrong:
Option ClassCastException: This exception is thrown when you attempt an invalid type cast between incompatible reference types, not when parsing strings. Option IllegalStateException: This indicates that a method has been invoked at an inappropriate time or in an illegal state, not that an input string has the wrong format. Option IOException: This is related to input or output errors such as file or network problems, and is unrelated to parsing a string that is already in memory.


Common Pitfalls:
One pitfall is assuming that parseInt returns a special value like 0 or -1 on error instead of throwing an exception. Another is catching a very broad exception type such as Exception instead of the more specific NumberFormatException, which can hide unrelated problems. Developers should also avoid calling parseInt without validation or exception handling when processing user input. For exam questions, it is important to remember that Integer.parseInt throws NumberFormatException when the input string cannot be interpreted as a valid integer.


Final Answer:
The code Integer.parseInt("two") throws a NumberFormatException, which is the appropriate type to use in the catch block.

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion