Java InputMismatchException

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

InputMismatchException is the most common exception that is experienced in Java. It is an unchecked exception since it resides in the subclass of java.lang.RuntimeException.

Introduction to Java InputMismatchException

The InputMismatchException is considered a runtime exception in Java since it is thrown by a scanner class or object to make the developer understand that the object does not match the expected type and is out of range. This exception is mostly thrown during the runtime when the developer passes input. Since it is an unchecked exception, the InputMismatchException doesn’t need to be declared in the constructor or method of the throws class. The hierarchy for the exception java.util.InputMismatchException is as follows:

hierarchy-for-exception-java-util-inputmismatchexception

It also provides all the methods that are provided by java.lang.Throwable class and the java.lang.Object class because it is the subclass of both of these classes.

Declaration

Constructor Summary of Java InputMismatchException

There are two constructors provided by the InputMismatchException class

MethodDescription
InputMismatchException()It constructs the exception, InputMismatchException with an error message as null in a string since it’s a default constructor.
InputMismatchException(e)It is known as a parameterized constructor of the exception InputMismatchedException that saves the reference to the error message e for retrieving the method getMessage().

Method Summary of Java InputMismatchException

There are two types of methods available in InputMismatchException of java. The first is inherited from java.lang.Throwable class and the one derived from java.lang.Object class. Following are the methods for this exception class in java:

Method nameMethod declarationDescription
addSuppressedpublic final void addSuppressed(Throwable exception)This method is used to append the exception that is specified to the exception that is suppressed for delivering this exception. It is a thread-safe method and is called automatically with the try-catch statement.
fillInStackTracepublic Throwable fillInStackTrace()It is used for filling the execution stack trace. It also returns the throwable instance of the reference.
getCausepublic Throwable getCause()It is used to return the cause of null or throwable in case the cause is not known.
getLocalizedMessagepublic String getLocalizedMessage()It is used to create and return the localized description of the throwable.
getMessagepublic String getMessage()It is used to return the detailed message of the typed string of this throwable.
getStackTracepublic StackTraceElement[] getStackTrace()It is used to return the array of a stack with all the trace elements that represent the stack trace.
getSuppressedpublic final Throwable[] getSuppressed()It returns all the suppressed exceptions in an array.
initCausepublic Throwable initCause(Throwable cause)It is used to initialize the cause of the throwable to a specified value and then return a reference to this throwable instance.
printStackTracepublic void printStackTrace()This method is used for printing the stack trace for this object throwable on the error output stream of the field System.err
setStackTracepublic void setStackTrace(StackTraceElement[] stackTrace)It is used for setting the stack trace elements.
toStringpublic String toString()It is used to return the string representation of this throwable object.
cloneprotected Object clone() throws CloneNotSupportedExceptionIt is used for creating and returning the copy of the specified object.
equalspublic boolean equals(Object obj)It is used to check if two objects are equal or not, returns true if same, or else false.
finalizeprotected void finalize() throws ThrowableThe garbage collector generally calls it to check if there are no more references to an object.
getClasspublic final Class<?> getClass()It is used for returning the class object that represents the object of the class in runtime.
hashCodepublic int hashCode()It is used to return the hash code value of an object.
notifypublic final void notify()It invokes a single thread waiting on the object.
notifyAllpublic final void notifyAll()It invokes all the thread waiting on the object.
waitpublic final void wait() throws InterruptedExceptionIt is used to make the current thread wait until another thread invokes the notify method.

Java InputMismatchException Examples

Write a java program to show InputMismatchException at the output.

Output

Explanation: In this example, the InputMismatchException is thrown when, instead of a number, a string is entered, which is “Hello”. This is because the scanner input was expecting an integer or a number in the input and not a string value, which was why the InputMismatchException was thrown at the output.

How to Avoid InputMismatchException?

In order to avoid InputMismatchException, make sure that the input that is entered for the scanner object is of a valid type, as mentioned in the code. If this exception is thrown, then the format or the type of input data should be checked and fixed to execute the program correctly. For instance, in the above example, if an integer instead of a string, “Hello,” is given as input to the scanner object, then the InputMismatchException does not occur, and the program is executed properly.

Conclusion

  • InputMismatchException is the most common exception experienced in Java. It is an unchecked exception since it resides in the subclass of java.lang.RuntimeException.
  • The exception InputMismatchException is considered a runtime exception in java since it is thrown by a scanner class or object to make the developer understand that the object does not match the expected object type and that it is out of range. This exception is mostly thrown during the runtime when the developer passes input.
  • The InputMismatchException doesn’t need to be declared in the constructor or method of the throws class since it is an unchecked exception.