Java Reference
In-Depth Information
try {
Scanner console = new Scanner(System. in) ;
System. out . print ( "Enter an integer: " );
a[0] = console.nextInt();
return true ;
}
catch (InputMismatchException exception)
{
return false ;
}
}
}
This time, the getNumber method takes as input the address of the array where the
result should be stored. The method returns true when it is successful in reading a integer.
If the call to the nextInt method raises an InputMismatchException , that is, the user did
not enter an integer, then false is returned. In the main method, the while loop has no
body. It keeps asking the user to enter an integer until the getNumber method returns true ,
that is, an integer is entered.
There are two types of exceptions in Java: checked and unchecked . Java forces us to handle
checked exceptions in some way. For example, FileNotFoundException is an example of
a checked exception. Every time we open a file, we need to handle the case when the
file could not be found, that is, when a FileNotFoundException is raised. Conversely,
InputMismatchException is an example of an unchecked exception. Java does not force us
to handle the exception in any way. However, we handled the exception in order to address
the case when the user enters a string instead of an integer. ArithmeticException and
ArrayIndexOutOfBoundsException are two other examples of unchecked exceptions. The
first exception can be raised when we divide by 0, while the second exception can be raised
when we access an index of an array that is out of bounds.
Sometimes, when we handle an exception, we want to report to the user that something
went wrong. When the printStackTrace method is called on the exception object, infor-
mation about the exception will be generated and printed. For example, if we add the line
exception.printStackTrace() to the catch part of our rewritten code, we will see the
following printout if the user did not enter an integer.
java . util . InputMismatchException
at java. util .Scanner.throwFor(Scanner. java:909)
at java . util .Scanner.next(Scanner. java:1530)
at java . util .Scanner. nextInt(Scanner. java:2160)
at java . util .Scanner. nextInt(Scanner. java:2119)
at Test . getNumber(Test . java :18)
at Test .main(Test . java :9)
This gives us detailed information about which line of which method was called when the
exception occurred. For example, the above code tells us that first Line 9 of the main method
was executed. This called the getNumber method. Line 18 of the getNumber method called
the nextInt method. In the nextInt method, an input mismatch exception has occurred.
There are three main ways to handle exceptions. The most obvious is to print the
stack trace and terminate the program. By the way, this is the default behavior for
unchecked exceptions that are not handled. A different alternative is to fix the error.
In our example, we repeatedly asked for input until input of the correct typed was
given. A third option is to log the error in a log file and continue program execution.
After the program terminates, the log file can be checked for possible errors during
the execution of the program.
 
Search WWH ::




Custom Search