Title: Lesson 9 Exceptions, File Input and Output
1Lesson 9Exceptions, File Input and Output
- Types of exceptions
- Error
- unrecoverable, not supposed to be handled by
users. - AWTError, LinkageError, ThreadDeath,
VirtualMachineError - RuntimeException
- ArithmeticException, IndexOutOfBoundsException,
NullPointerException, IOException. . . - UserException
2- The hierarchy of the relevant classes
3(No Transcript)
4- The try-catch-final statement for exception
handling
try statements //possibly generate
exceptions catch (ltexceptiongt e)
statements //Exception handling
code . . . //Other catch segments finally
//Optional, always executed statements
5Execution of the try-catch-final statement
- First execute the try block.
- If an exception occurs, the execution of the try
block is terminated. An object of the
corresponding class was thrown . - If the object is an error object, the execution
of the program is terminated. - If the object matches with one of the exceptions
specified in a catch segment, the catch block is
executed. - The finally block is optional. But if it exists,
it is always executed, even when there is no
exception occurs. - The execution moves to the statement after the
try-catch-finally.
6int a null //a null //a new
int10 //a new int10 int n 0
//n 1 //n 10 //n
9 System.out.println("0 n " n) try
an 100 / n System.out.println( "0
a" n " " an) catch
(ArithmeticException e1) System.out.println(
"1 " e1) catch (NullPointerException e2)
System.out.println("2 " e2) catch
(IndexOutOfBoundsException e3) System.out.print
ln("3 " e3) finally System.out.printl
n("4 Bye-bye") System.out.println("0 Run
through")
0 n 0 1 java.lang.ArithmeticException / by
zero 4 Bye-bye 0 Run through
0 n 1 2 java.lang.NullPointerException 4
Bye-bye 0 Run through
0 n 10 3java.lang.ArrayIndexOutOfBoundsExcepti
on 10 4 Bye-bye 0 Run through
0 n 9 0 a9 11 4 Bye-bye 0 Run through
7- In last example, if there is no need to handle
the exceptions individually, we can replace the
three catches with a single catch block. - A user may defines UserException subclasses,
throws objects of these new classes when certain
conditions occur during execution, and catches
them.
catch (RuntimeException e) System.out.println
( e)
8Order of catches
- If a method does not catch an exception, the
execution of the method will be cut short. The
exception object is then thrown out to the caller
of the method level by level upward. - v.main() catch
-
- a.f1() no catch
-
- x.f2() Exception occurs, no catch
- If a user does not catch an RuntimeException
object, Java exception handler will eventually
catch it and terminate the execution.
9Simple file input/output
- In physical world, a stream is a continuous flow
of water - In java, a byte stream is a sequence of bytes.
- An input stream is a source from which bytes are
read one by one. Eg, a keyboard, a disk file
opened for reading. - An output stream is a destination to which bytes
are written one by one. Eg, a monitor, a printer,
a disk file opened for writing.
10- A disk file for reading in a program is mapped to
an object of FileInputStream. - A disk file for writing in a program is mapped to
an object of FileOutputStream.
11(No Transcript)
12(No Transcript)
13(No Transcript)
14- FileInputStream( String fileName)
- Creates a FileInputStream object and maps it to
an actual disk file for reading //Throws
FileNotFoundException - int available() //Throws IOException
- Returns the number of bytes left in the file
that can be read. (Decrement by 1 after a byte is
read.) - int read() //Throws IOException
- Reads a byte of data from the file. Returns
-1 if the end of file is reached. - void close() //Throws IOException
- Closes the file.
15- A program that prints the contents of a file on
screen
try FileInputStream r new FileInputStream(
e.dat) int a r.available() int
nByte 0 int d r.read() while ( d
! -1) nByte System.out.print(
(char) d) d r.read() r.close()
System.out.println( "\nnByte " nByte)
System.out.println( "r.available() " a
) catch ( IOException e)
System.out.println("\n IO Error " e)
. . . ...File contents... . . . nByte
755 r.available() 755
DisplayFile
16- Class FileOutputStream
- FileOutputStream( String fileName)
- Creates an OutputFileStream object and maps it
to the disk file with the specified name for
writing. - Void write( int b)
- Writes the specified byte to the file.
- Void close( )
- The exceptions thrown are similar to that of
FileInputStream.
17- This program saves the characters typed on a
keyboard into a disk file. (The end is indicated
by typing a ltcrtl-dgt.)
try FileOutputStream r new FileOutputStream(
sam.dat) int nByte 0 int d
System.in.read() while( d ! 4) // 4 is
the code for ltcrtl-dgt nByte r.write(
d) d System.in.read() r.close() catc
h ( IOException e) System.out.println("\n
IO Error " e)
SaveFile
C\gtjava SaveFile I love Hong Kong. Bye-bye. C
\gttype sam.dat I love Hong Kong. Bye-bye.