Exceptions in Java: Christian Ratliff Sr. Technology Architect Core Libraries Group Delorme Mapping
Exceptions in Java: Christian Ratliff Sr. Technology Architect Core Libraries Group Delorme Mapping
Operation
A method which can possibly raise an exception.
Invoker
A method which calls operations and handles resulting exceptions.
Exception
A concise, complete description of an abnormal event.
Raise
Brings an exception from the operation to the invoker, called throw in
Java. Another very common word for this is emit.
Handle
Invoker’s response to the exception, called catch in Java.
Backtrack
Ability to unwind the stack frames from where the exception was raised to
the first matching handler in the call stack.
Copyright (c) 2001 DeLorme 4
Lexicon: Types of Exceptions
Hardware
Generated by the CPU in response to a fault (e.g. divide by zero, overflow,
segmentation fault, alignment error, etc).
Software
Defined by the developer to represent any other type of failure. These
exceptions often carry much semantic information.
Domain Failure
The inputs, or parameters, to the operation are considered invalid or
inappropriate for the requested operation.
Range Failure
Operation cannot continue, or output is possibly incorrect.
Monitor
Describes the status of an operation in progress, this is a mechanism for
runtime updates which is simpler than subthreads.
Parameterized forms:
Address of a subroutine to be called during an error.
Name of a statement labels to jump to on error.
Status variables changed by the operation, and then checked by
invoker at operation return.
Language-based forms:
Fault subroutines tied to a type or instance.
Fault target defined at compile time on the subroutine.
PL/I condition statements.
throws
Describes the exceptions which can be raised by a method.
throw
Raises an exception to the first available handler in the call stack,
unwinding the stack along the way.
try
Marks the start of a block associated with a set of exception handlers.
catch
If the block enclosed by the try generates an exception of this type,
control moves here; watch out for implicit subsumption.
finally
Always called when the try block concludes, and after any necessary catch
handler is complete.
try {
oClass.setProperty(“foo”);
oClass.doSomeWork();
try { /* example 2 */
myObject.setName(“foo”);
} catch (Exception e) {
System.err.println(“Exception in anotherMethod() “+e.toString());
return false;
} finally {
/* If the close operation can raise an exception, whoops! */
if (myClass.close() == false) {
break;
}
}
return false;
}
} finally {
continue;
}
} /* end of while */
}
Every try block must have at least one catch or finally block
attached.
If an exception is raised during a try block:
The rest of the code in the try block is skipped over.
If there is a catch block of the correct, or derived, type in this stack
frame it is entered.
If there is a finally block, it is entered.
If there is no such block, the JVM moves up one stack frame.
If no exception is raised during a try block, and there is no
System.exit() statement:
If there is a matching finally block it is entered.
??
j a v a .la n g .T h r o w a b l e
j a v a .la n g .E r r o r j a v a .la n g . E x c e p t io n
/* The correct structure is to arrange the catch blocks with the most
* derived class first, and base class at the bottom. */
try {
Socket oConn = new Socket(“www.sun.com”, 80);
} catch (UnknownHostException ohe) {
} catch (IOException ioe) {
}
if (oStatement.executeUpdate() != 1) {
throw new CreateException(“Account could not be created.”);
}
if (oStatement.executeUpdate() != 1) {
throw new CreateException(“Account could not be created.”);
}
/* If this rethrow was missing, we would have a problem. */
} catch (CreateException ce) {
throw ce;
} catch (SQLException sqle) {
throw new CreateException(“SQL error in creating: “+sqle.toString());
}
???
If a finally block is in use, then each statement which can exit the
try block has a special bytecode instruction attached to it (jsr).
This calls the special finally subroutine.
The larger the path analysis graph, the more difficult it is for the
JIT compiler to optimize the Java application.
11.2.2
The runtime exception classes (RuntimeException and its subclasses) are
exempted from compile-time checking because, in the judgment of the
designers of Java, having to declare such exceptions would not aid
significantly in establishing the correctness of Java programs. Many of the
operations and constructs of the Java language can result in runtime
exceptions. The information available to a Java compiler, and the level of
analysis the compiler performs, are usually not sufficient to establish that
such runtime exceptions cannot occur, even though this may be obvious
to the Java programmer. Requiring such exception classes to be declared
would simply be an irritation to Java programmers.
For example, certain code might implement a circular data structure that, by
construction, can never involve null references; the programmer can then
be certain that a NullPointerException cannot occur, but it would be
difficult for a compiler to prove it. The theorem-proving technology that is
needed to establish such global properties of data structures is beyond the
scope of this Java Language Specification.
class java.lang.ThreadGroup {
/* lots of other methods deleted… */
public void uncaughtException(Thread t, Throwable e);
}
/* The thread startup will change to this: */
new Thread(oMyThreadGroup, oMyThread).start();
You can derive from ThreadGroup, and make your own class with its
own implementation of uncaughtException().
Then you can implement the listener pattern in your derived class and
broadcast exception events to interested parties.
Owing to the presenters limited experience with RMI, David Ezzio will
speak for a moment on this interesting problem.
????