Java Reference
In-Depth Information
The program includes a
throws
clause with an
IOException
for the
main()
method. Can you run the
ReadInput2Test3
class as you have been running other classes using the
java
command? Yes. You can run the
ReadInput2Test3
class the same way you run other classes in Java. The requirement to run a class is that it should
include a
main()
method, which is declared as
public static void main(String[] args)
. The requirement does
not specify anything about a
throws
clause. A
main()
method, which is used to run a class as a starting point, may
or may not contain a
throws
clause.
Suppose you run the
ReadInput2Test3
class and the call to the
System.in.read()
method in the
readChar()
method of the
ReadInput2
class throws an
IOException
. How will the
IOException
be handled and what will
handle it? When an exception is thrown in a method body, the runtime checks if the code throwing the exception
is inside a
try-catch
block. If the exception throwing code is inside a
try-catch
block, the Java runtime looks for
the
catch
block that can handle the exception. If it does not find a
catch
block that can handle the exception, or
the method call is not inside a
try-catch
block, the exception is propagated up the method call stack. That is, the
exception is passed to the caller of the method. In your case, the exception is not handled in the
readChar()
method
of the
ReadInput2
class. Its caller is the piece of code in the
main()
method of the
ReadInput2Test2
class. In this
case, the same exception is thrown at the point where the
ReadInput2.readChar()
method call is made inside the
ReadInput2Test2.main()
method. The runtime applies the same checks to handle the exception. If you run the
ReadInput2Test2
class and an
IOException
is thrown, the runtime finds that the call to
ReadInput2.readChar()
is inside a
try-catch
block, which can handle the
IOException
. Therefore, it will transfer the control to the
catch
block, which handles the exception, and the program continues in the
main()
method of the
ReadInput2Test2
class.
It is very important to understand that the control does not go back to the
ReadInput2.readChar()
method after it
throws an exception and the exception is handled inside the
ReadInput2Test2.main()
method.
When you run the
ReadInput2Test3
class, the call to the
ReadInput2.readChar()
method is not inside a
try-catch
block. In this case, the Java runtime will have to propagate the exception up the method call stack. The
main()
method is the beginning of the method call stack for a Java application. This is the method where all Java
applications start. If the
main()
method throws an exception, the runtime handles it. Recall that if the runtime
handles an exception for you, it prints the call stack details on the standard error and exits the application.
Recall that a
catch
block with an exception type can handle an exception of the same type, or any of its
subclass type. For example, a
catch
block with
Throwable
exception type is capable of handling all types of
exceptions in Java, because the
Throwable
class is the superclass of all exception classes. This concept is also
applicable to the
throws
clause. If a method throws a checked exception of
Exception1
type, you can mention
Exception1
type in its
throws
clause or any of the superclasses of
Exception1
. The reasoning behind this rule is
that if the caller of the method handles an exception that is the superclass of
Exception1
, the same handler can also
handle
Exception1
.
■
the Java compiler forces you to handle a checked exception either by using a
try-catch
block or by using a
throws
clause in the method or constructor declaration. if a method throws an exception, it should be handled somewhere
in the call stack. that is, if a method throws an exception, its caller can handle it, or its caller's caller can handle, and so
on. if an exception is not handled by any callers in the call stack, it is known as an uncaught exception (or an unhandled
exception). an uncaught exception is finally handled by the Java runtime, which prints the exception stack trace on the
standard error and exits the Java application. a different behavior may be specified for uncaught exceptions in a thread.
please refer to the chapter on threads in the topic
Beginning Java Language Features
(iSBn 978-1-4302-6658-7) for
more details on how to specify an exception handler for a thread.
Tip