Programming with Java - Lake

advertisement
COP 2800
Lake Sumter State College
Mark Wilson, Instructor
Exceptions
 Methods
with unknown side effects
• Versioning
• Latent bug
 Methods
that may not work at runtime
• Unavailable system resources
• Abnormal or unexpected response
 An
event that occurs during the execution of a
program that disrupts the normal flow of
instructions
 Method Java uses to handle errors and other
exceptional events
 An exception object, contains information about
the error, including its type and the state of the
program when the error occurred
 Creating an exception object and handing it to
the runtime system is called throwing an
exception.
 Traditional
processing breaks well formed
constructs, makes spaghetti code
 The runtime system searches the call stack for
a method that contains a block of code that can
handle the exception (exception handler)
 An exception handler is considered
appropriate if the type of the exception object
thrown matches the type that can be handled
by the handler
 Uncaught exceptions cause the runtime system
(and, therefore, the program) to terminate
 Separates
Code
Error-Handling Code from "Regular"
• In traditional programming, error detection,
reporting, and handling often lead to confusing
spaghetti code
 Propagating
Errors Up the Call Stack
• Java runtime environment searches backward
through the call stack to find any methods that are
interested in handling a particular exception
 Grouping
and Differentiating Error Types
• grouping or categorizing of exceptions is a natural
outcome of the class hierarchy
 Checked
Exception – Exception class
• well-written application anticipates and recovers
 Error
– Error class
• external to the application
• application usually cannot anticipate or recover
 Runtime
class
Exception – RuntimeException
• internal to the application
• application usually cannot anticipate or recover
 Error
and runtime exceptions are unchecked
exceptions

throws
• Method declares it may throw an exception

try
• Sets off the code block for risky behavior

catch
• Sets off code to handle an exception
• Follows try
• Multiple catches possible

finally
• Always executes after the try block
• May not execute if the JVM exits during a try or catch
• Good place to put cleanup code (e.g. close open files)
public void writeList() {
PrintWriter out = null;
try {
System.out.println("Entering" + " try statement");
out = new PrintWriter(new FileWriter("OutFile.txt"));
for (int i = 0; i < SIZE; i++)
out.println("Value at: " + i + " = " + vector.elementAt(i));
}
catch (ArrayIndexOutOfBoundsException e) {
System.err.println("Caught ArrayIndexOutOfBoundsException: "
+ e.getMessage());
}
catch (IOException e) {
System.err.println("Caught IOException: " + e.getMessage());
}
finally {
if (out != null) {
System.out.println("Closing PrintWriter");
out.close();
}
else {
System.out.println("PrintWriter not open");
}
}
}
 Methods
that don’t catch checked exceptions
must specify that they can throw exceptions
 Identifies exceptions that may need to be
caught
 Unchecked exceptions are optional
public void writeList() throws IOException, ArrayIndexOutOfBoundsException {
public void writeList() throws IOException {




Any code can throw exceptions
Exceptions are thrown with throw
All exceptions are descendants of Throwable
Most programs throw and catch objects derived from
the Exception class
public Object pop() {
Object obj;
if (size == 0) {
throw new EmptyStackException();
}
obj = objectAt(size - 1);
setObjectAt(size - 1, null);
size--;
return obj;
}






Exception type isn't represented by those in the Java
platform?
Help users differentiate your exceptions from other
vendors?
Does your code throw more than one related
exception?
Will users have access to other’s exceptions?
Most applications will throw objects that are
Exceptions. Errors are normally used for serious, hard
errors in the system
For readable code, it's good practice to append the
string Exception to the names of all classes that inherit
(directly or indirectly) from the Exception class.











A program can use exceptions to indicate that an error occurred.
To throw an exception, use the throw statement and provide it with an
exception object — a descendant of Throwable — to provide information
about the specific error that occurred.
A method that throws an uncaught, checked exception must include a
throws clause in its declaration.
A program can catch exceptions by using a combination of the try, catch,
and finally blocks.
The try block identifies a block of code in which an exception can occur.
The catch block identifies a block of code, known as an exception handler
The finally block identifies a block of code that is guaranteed to execute,
The try statement should contain at least one catch block or a finally block
and may have multiple catch blocks.
The class of the exception object indicates the type of exception thrown.
The exception object can contain further information about the error
With exception chaining, an exception can point to the exception that
caused it
Packages, Jars and Deployment.
Oh, my!
Beginning GUI
Download