D:\cs151/chap80>java Square Enter an integer: Rats Exception in thread "main" java.lang.NumberFormatException: Rats at java.lang.Integer.parseInt(Integer.java:409) at java.lang.Integer.parseInt(Integer.java:458) at Square .main(NFException.java:18) D:\cs151/chap80>
The user entered Rats which parseInt
could
not convert to an integer.
Nothing is wrong with the program.
The problem is that parseInt
cannot convert
"Rats" into an int
.
When parseInt
found the problem it
threw a NumberFormatException
.
An exception is a problem that occurs
when a program is running.
Often the problem is caused by circustances outside
the control of the program,
for example, bad user input or a bad sector in a disk file.
A Java program may have statements that
catch exceptions.
When an exception occurs,
the Java virtual machine creates an object
of class Exception (big 'E') which holds information about the problem.
The Exception
object is used to recover from the problem.
An error, also, is a problem that occurs when a program is running. An error is represented by an object of class Error. But an error is too severe for a program to handle. The program must stop running.