SCOUG Java Programming Class 4
Overview
Exception handling is built into the Java language via the try{..} catch{..} finally{..} statements and is intended to be used for types of processing situations that normally can be handled by the application in some meaningful way.
Certain methods can create exceptions. Whenever you reference one of these methods, you must enclose it in a try code-block. Then you must also code a catch code-block to process the exception. This code block will receive an exception object as a parameter and can perform whatever processing is necessary. Example:
.. try { File readFile = new File(sDir, sFile); DataInputStream dis = new DataInputStream( new FileInputStream(readFile) ); .. } catch (Exception e) { statusField.setText(e.toString()); .. }
In the above code, either of the two statements inside the try block (or possibly further statements) could throw an exception. The catch block will be invoked in all of these cases and simply stores the exception text into a textfield. Additional processing can also be performed here.
Exception processing:
public void myMethod(int x, String fileName) throws ArithmeticException, IOException { if (x == 0) throw new ArithmeticException(); if (fileName == null) throw new IOException(); .. }
Errors are another type of extension to the throwable class and are derived from the java.lang.Error class. They are used in situations for which there is normally not a simple recovery that the application can perform and still continue to execute. They are thus not usually handled by the application (although if you wish to handle them, use the same procedure as for exceptions).
The classes in the IO package can be used for manipulating data-streams and files and also performing operating system operations on files and directories. Due to security restrictions imposed on Applets (especially when running in a browser environment) these classes are mostly used by Java Applications.
This package contains classes which allow a Java program to communicate, via tcpip protocol, with other machines and to process content received from those machines. Supported processing modes include:
Best example of using these classes is the HotJava browser.
The IO package contains the classes and interfaces for handling files, streams and operating system related operations (directory manipulation, etc)
type |
name |
extends |
class |
BufferedInputStream |
FilterInputStream |
class |
BufferedOutputStream |
FilterOutputStream |
class |
ByteArrayInputStream |
InputStream |
class |
ByteArrayOutputStream |
OutputStream |
class |
DataInputStream |
FilterInputStream, extends DataInput |
class |
DataOutputStream |
FilterOutputStream, extends DataOutput |
class |
File |
|
class |
FileDescriptor |
|
class |
FileInputStream |
InputStream |
class |
FileOutputStream |
OutputStream |
class |
FilterInputStream |
InputStream |
class |
FilterOutputStream |
OutputStream |
abstract |
InputStream |
|
class |
LineNumberInputStream |
FilterInputStream |
abstract |
OutputStream |
|
class |
PipedInputStream |
InputStream |
class |
PipedOutputStream |
OutputStream |
class |
PrintStream |
FilterOutputStream |
class |
PushbackInputStream |
FilterInputStream |
class |
RandomAccessFile |
implements DataOutput, DataInput |
class |
SequenceInputStream |
InputStream |
class |
StreamTokenizer |
|
class |
StringBufferInputStream |
InputStream |
interface |
DataInput |
|
interface |
DataOutput |
|
interface |
FilenameFilter |
|
The NET package contains the classes and interfaces which support tcpip networking by Java programs.
type |
name |
extends |
abstract |
ContentHandler |
|
class |
DatagramPacket |
|
class |
DatagramSocket |
|
final |
InetAddress |
|
class |
ServerSocket |
|
class |
Socket |
|
abstract |
SocketImpl |
|
class |
URL |
|
abstract |
URLConnection |
|
class |
URLEncoder |
|
abstract |
URLStreamHandler |
|
interface |
ContentHandlerFactory |
|
interface |
SocketImplFactory |
|
interface |
URLStreamHandlerFactory |
|
The following table lists the exceptions thrown by the standard JDK classes:
Class |
Exceptions |
awt |
AWTException |
io |
EOFException extends IOException |
|
FileNotFoundException extends IOException |
|
IOException extends java.lang.Exception |
|
InterruptedIOException extends IOException |
|
UTFDataFormatException extends IOException |
lang |
ArithmeticException extends RuntimeException |
|
ArrayIndexOutOfBoundsException extends IndexOutOfBoundsException |
|
ArrayStoreException extends RuntimeException |
|
ClassCastException extends RuntimeException |
|
ClassNotFoundException extends Exception |
|
CloneNotSupportedException extends Exception |
|
Exception extends Throwable |
|
IllegalAccessException extends Exception |
|
IllegalArgumentException extends RuntimeException |
|
IllegalMonitorException extends RuntimeException |
|
IllegalThreadException extends RuntimeException |
|
IndexOutOfBoundsException extends RuntimeException |
|
InstantiationException extends Exception |
|
InterruptedException extends Exception |
|
NegativeArraySizeException extends RuntimeException |
|
NoSuchMethodException extends Exception |
|
NullPointerExceptioin extends RuntimeException |
|
NumberFormatException extends IllegalArgumentException |
|
RuntimeException extends Exception |
|
SecurityException extends RuntimeException |
|
StringIndexOutOfBoundsException extends IndexOutOfBoundsException |
net |
MalformedURLException extends IOException |
|
ProtocolException extends IOException |
|
SocketException extends IOException |
|
UnknownHostException extends IOException |
util |
EmptyStackException extends RuntimeException |
|
NoSuchElementException extends RuntimeException |
The following table lists the Errors:
Class |
Error(s) |
awt |
AWTError extends Error |
lang |
AbstractMethodError extends IncompatibleClassChangeError |
|
ClassCircularityError extends LinkageError |
|
ClassFormatError extends LinkageError |
|
Error extends Throwable |
|
IllegalAccessError extends IncompatibleClassChangeError |
|
IncompatibleClassChangeError extends LinkageError |
|
InstantiationError extends IncompatibleClassChangeError |
|
InternalError extends VirtualMachineError |
|
LinkageError extends Error |
|
NoClassDefFoundError extends LinkageError |
|
NoSuchFieldError extends IncompatibleClassChangeError |
|
NoSuchMethodError extends IncompatibleClassChangeError |
|
OutOfMemoryError extends VirtualMachineError |
|
StackOverflowError extends VirtualMachineError |
|
ThreadDeath extends Error |
|
UnknownError extends VirtualMachineError |
|
UnsatisfiedLinkError extends LinkageError |
|
VerifyError extends LinkageError |
|
VirtualMachineError extends Error |
Java and JDK as referenced herein are protected by the following copyright notices:
(c) Copyright Sun Microsystems, Inc., 1992-1997. All rights reserved.
(c) Copyright IBM Corporation, 1996-1997. All rights reserved.
The JDK is based in part on the work of the Independent JPEG group.
The Java(tm) technology is owned and exclusively licensed by Sun Microsystems, Inc. Java(tm) is a trademark of Sun Microsystems, Inc. in the United States and other countries.
The JDK implementation used in this class is OS/2 version 1.0.2 and is used with permission of IBM Corporation. This use is governed by the terms and conditions specified in the beta license agreement provided by IBM Corporation.