Java Reference
In-Depth Information
Exceptions of Type Error
The exceptions that are defined by the Error class and its subclasses all represent conditions that you aren't
expected to do anything about, so you aren't expected to catch them. Error has several direct subclasses
including ThreadDeath , LinkageError , and VirtualMachineError . These are unchecked exceptions. You
can do little or nothing to recover from these types of errors during the execution of the program. All you
can hope to do (if you're lucky) is read the error message that is generated by the exception being thrown
and then, particularly in the case of a LinkageError exception, try to figure out what might be wrong with
your code to cause the problem.
Exceptions of Type RuntimeException
Almost all the exceptions that are represented by subclasses of Exception are checked exceptions so you
must include code in your programs to deal with them if your code may cause them to be thrown. In methods
whose code has the potential to throw a checked exception, you must either handle the checked exception
within the method or register that your method may throw such an exception. If you don't, your program
does not compile. You see in a moment how to handle exceptions and how to specify that a method can
throw an exception.
Exceptions of type RuntimeException are exempt from the requirement to catch them, even though
RuntimeException is derived from Exception . The reason that the compiler allows you to ignore
RuntimeException exceptions is that they generally arise because of serious errors in your code. In most
cases you can do little to recover the situation. However, in a few instances, it may be useful to include code
to recognize them.
You need to know when these various types of exception can be thrown and how you can handle them.
You try out some of the RuntimeException exceptions later in the chapter, as some of them are very easy
to generate, but let's see what other sorts of exception classes have Exception as a base.
Other Subclasses of Exception
Search WWH ::




Custom Search