What type of exception is thrown by parseInt() if it gets illegal data?

  1. ArithmeticException

  2. RunTimeException

  3. NumberFormatException

  4. NumberError


Correct Option: C
Explanation:

To solve the question, the user needs to know what parseInt() method does. The parseInt() method is used to convert a string to an integer. If the string argument cannot be parsed as an integer, parseInt() throws a NumberFormatException.

Now, let's go through each option and explain why it is right or wrong:

A. NumberError: There is no exception called NumberError in Java. This option is incorrect.

B. ArithmeticException: The ArithmeticException is thrown when an exceptional arithmetic condition has occurred. Since parseInt() throws a NumberFormatException, this option is incorrect.

C. RunTimeException: RuntimeException is a superclass of all classes that represent exceptional conditions that occur during the execution of Java code. Since NumberFormatException is a subclass of RuntimeException, this option is partially correct. However, the specific exception thrown by parseInt() is NumberFormatException.

D. NumberFormatException: This is the correct answer. When parseInt() gets illegal data, it throws a NumberFormatException. This exception is thrown to indicate that the application has attempted to convert a string to one of the numeric types, but that the string does not have the appropriate format.

Therefore, the answer is: D. NumberFormatException.

Find more quizzes: