An exception is an object that describes an unusual
or erroneous situation
Exceptions are thrown by a program, and may be caught
and handled by another part of the program
Exception hierarchy
Errors:
Errors are fairly rare and usually fatal. They are
caused by bugs in the Java VM, or by the program running out of memory
or other resources.
Errors are usually not handled by programs.
Exceptions:
Exceptions can be caused by bugs in the program or
improper data supplied to the program, e.g., illegal characters in numeric
data, division by zero (for integer), array index out of bounds, or file
not found.
Exceptions should be handled, or caught
An exception is either checked or unchecked
Checked exceptions must be caught or declared as
thrown.
You are not required to handle unchecked exceptions.
Exception Handling
Try-catch block and finally block
try {
// code may cause/throw exceptions
} catch (Exception1 e1) {
// handle Exception1
} catch (Exception2 e2) {
// handle Exception2
} finally {
// optional
// code always executed whether
an exception is thrown or not
}
Define your own exception
public class MyException extends Exception
{
public MyException(String msg)
{
super(msg);
}
}
Throw statement and throws clause
public class MyClass {
public void aMethod(int i,
int j)
throws MyException
{
// ...
throw new
MyException(reason);
// ...
}
}
I/O Streams
A stream is a sequence of bytes.
In a program, we read information from an input stream
and write information to an output stream
An I/O stream is either a
character stream, which deals with text data
byte stream, which deal with binary data
Stream-based I/O supports reading or writing data
sequentially. A stream may be opened for reading or writing, but not reading
and writing.
Random access I/O supports reading and writing data
at any positions of a file. A random access file may be opened for reading
and writing.
There are three standard I/O streams:
standard input – defined by System.in
standard output – defined by System.out
standard error – defined by System.err
Reading/writing text files
Use reader and and writer classes for text I/O. Handles
conversion between Unicode and native character encoding
May throw IOException
BufferedReader in
= new BufferedReader(
new FileReader("foo.in"));
PrintWriter out
= new PrintWriter(
new BufferedWriter(
new FileWriter("foo.out")));
Set up ODBC data source on Windows 9x/NT/2000 with
Access 97/2000
Start Control Panel.
Double click on ODBC Data Sources (32 bit).
On ODBC Data Source Administrator panel, click
on Add.
On Create New Data Source panel, select Microsoft
Access Driver (*.mdb), and click on Finish.
On ODBC Text Setup panel, fill in the Data
Source Name. For this example, it should be Users.
Click on Create for a new database (or Select for an existing
database).
On New Database panel, choose a file name,
e.g,. Useers.mdb.
Click on OK.
Back to ODBC Text Setup panel, click on OK.
Make sure that the new data source name appears in
the ODBC Data Source Administrator panel, and click on OK.