Java provides exception handling.
An exception is an object that is an instance of some subclass of java.lang.Throwable. Throwable has two standard subclasses java.lang.Error and java.lang.Exception
InputFile.java:11: Exception java.io.FileNotFoundException
must be caught, or it must be declared in the throws clause
of this method.
in = new FileReader(filename);
^
This message indicates that the compiler found an exception that is not
being handled.
The Java language requires that a method either catch
all "checked" exceptions (those that are checked by the runtime system)
or specify that it can throw that type of exception.
For standard java provided classes, one can see when a method throws an exception (and which) by its definition
public static native void sleep(long millis) throws InterruptedException
The idea is to concentrate on the errors that can happen within contexts (i.e., handle it within the specific algorithm).
The main section in this is "Catching and Handling Exceptions" - I like the organization of the old version better
In brief:
try
The try clause simply establishes a block of code that is to have its exceptions and abnormal exits (through break, continue, return or exception propogation) handled. The try clause by itself does not do anything interesting; it is the catch and finally clauses that do exception handling and clean- up operations
A try block may be followed by zero or more catch clauses that specify code to handle various types of exceptions. (A try statement must be accompanied by at least one catch block or one finally block.) catch clauses have an unusual syntax: each is declared with an argument, much like a method argument. This argument must be of the type Throwable or a subclass.
When an exception occurs, the first catch clause that has an argument of the appropriate type is invoked. The type of the argument must match the type of the exception object, or it must be a subclass of the exception. This catch argument is valid only within the catch block, and refers to the actual exception object that was thrown.
Here is an example illustrating the exception hierarchy test
finally
The finally clause is generally used to clean up after the try clause. What is useful in the finally, is
that the code in a finally is guaranteed to be executed, if any portion of the try block is executed,
regardless of how the code in the try block completes.
Normal: control reaches end of try and then does finally.
If control reaches try because of a return, continue, or break, finally is executed before control
transfers to its new destination.
If an exception occurs in the try block and there is a local catch, control goes first to the catch and
then to finally.
If there is not a local catch, control transfers first to the finally, then propogates to the nearest catch
Java provides several predefined exceptions:
Common exceptions: these are from java.lang
Actually both of these are stupid - use %
Make it yourself exception handling:
Examples of use (silly example of bad code through-out )
a RuntimeException typically integer division by zero
try to access something that has not been instantiated
(a null object)
attempt to change a class to another that affects references and/or methods in other objects.
This is a superclass of a group of related errors - signals some kind of illegal use of a legal class.
invalid casting of an object
allocate an array with fewer than zero elements
out of memory and garbage collection can't help
late binding - could reference a class that was not defined
signals a class to be loaded could not be found
obvious
access to native methods that do not exist. Java cannot satisfy all of the links in a class that
it has loaded
"this error is reserved for events that should not be occurring" Internal error in the Java
compiler.
signals that a thread should terminate. It is thrown when Thread.stop() is called. Unusual -
does not print a message or cause interpreter to exit. You may catch it to do any necessary cleanup
for a thead, but if you do, you must re-throw the error so that the tread actually terminates.
What happens?
A try block (even one command call - check out the reflection package java.lang.reflect.Method invoke()) could cause more than one type of exception
"Throwing Exceptions"
The Java runtime system and many classes from Java packages throw exceptions under some circumstances by using the throw statement.
You can use the same mechanism to throw exceptions in your Java programs. This section shows you how to throw exceptions from your
Java code.
An example:
If you want to catch your exception, you need to use try
"Runtime Exceptions--The Controversy"
Although Java requires that methods catch or specify checked exceptions, they do not have to catch or specify runtime exceptions, that is,
exceptions that occur within the Java runtime system. Because catching or specifying an exception is extra work, programmers may be
tempted to write code that throws only runtime exceptions and therefore doesn't have to catch or specify them. This is "exception abuse" and
is not recommended. The last section in this lesson, explains why.