Week 5 Slides
Week 5 Slides
Exceptions and
files
1
Exceptions
• Exceptions in Java represent problems that arise
during execution, which a program might want to
deal with (manage / handle)
2
Exceptions
• Whenever anything goes wrong in Java, an Exception
is generated:
• array[-1] = 0 gives an
ArrayIndexOutOfBoundsException
3
Exceptions
• Remember these are objects:
• They contain information about what went wrong
and where in the program it went wrong
• They have methods you can use to find out this
information
• They're created automatically when problems
occur (you can also define your own! later)
• You can even define variables of these types:
ArithmeticException ae = new ArithmeticException();
4
Exception methods
• Every Exception object has these methods:
• String getMessage()
• Find out more about what happened
• void printStackTrace()
• Print out where the program was at the time
this Exception object was created
• String toString()
• The name of the exception and its message
5
Exception methods
• You can set the message when you construct your
own Exception object:
• See ExceptionExample.java
6
Exceptions vs Errors
• There’s another class of objects called “Error” in Java
7
Exception hierarchy
• The built-in types of Exceptions form a "hierarchy"
• Class hierarchies will be covered next week
Exception
IOException RuntimeException
8
Exceptions are not “returned”
• You don't normally pass Exceptions around in the
normal way ExceptionExample.java was not an
accurate representation of how we use them
9
Exceptions are “thrown”
• So when something nasty happens, you can “throw”
an exception, using the throw keyword:
throw new Exception("Bail out!!");
• See ExceptionThrowExample.java
10
Exceptions are “thrown”
• Notice in the ExceptionThrowExample.java file:
• all methods that either throw an exception or call
a method that throws an exception have this
statement at the end of the signature:
11
Catching exceptions
• Exceptions can be caught and handled without
terminating the program
https://shovik.com/blog/3-improve-exceptions-in-ruby 12
Catching exceptions
• Exceptions can be caught and handled without
terminating the program:
try {
...statements...
}
catch(TypeOfException e) {
...do this if one of those Exceptions happens...
}
…continue here…
…(unless some un-caught exception occurs)…
• See ExceptionCatchExample.java
13
Catching specific exceptions
• You can choose to catch only one specific kind of
Exception:
try {
...statements...
}
catch(ArithmeticException ae) { .... }
//Other kinds will not be caught at all
14
Catching specific exceptions
• Or handle different types in different ways:
try {
...statements...
}
catch(ArithmeticException ae) { ..handle one way.. }
catch(IOException ioe) { ..handle some other way.. }
15
Catching specific exceptions
• Or handle different types in different ways:
try {
...statements...
}
catch(ArithmeticException ae) { ..handle one way.. }
catch(IOException ioe) { ..handle some other way.. }
16
Catching specific exceptions
• Handling a type of Exception will also cover all other
types beneath it in the hierarchy
try {
...statements...
}
catch(Exception e) {
...will catch absolutely everything... }
17
Catching specific exceptions
• Handling a type of Exception will also cover all other
types beneath it in the hierarchy
try {
...statements...
}
catch(Exception e) {
...will catch absolutely everything... }
18
Execution flow in try-catch
try {
....
..Exception not thrown
....
} catch (Exception e) {
....
....
....
}
...continues...
...
19
Execution flow in try-catch
try {
....
..Exception not thrown
....
} catch (Exception e) { When no exception is thrown in the try
.... block, the program skips over the catch
.... blocks and continues the execution of
.... the code below the try-catch
}
...continues...
...
20
Execution flow in try-catch
try {
....
..Exception thrown
....
} catch (Exception e) { When an exception is thrown in the try
.... block, the first catch block “catching”
.... the exception will execute, and then
.... the program continues the execution
} of the code below the try-catch
...continues...
...
21
finally
• You can add a finally block at the end of your try-
catch this block is always executed, no matter
what happens
try {
...statements...
}
catch(Exception e) {
...will catch absolutely everything... }
finally{
… do this every time, whether try successfully completed or
an exception was thrown and caught…
}
22
Some exceptions don’t need to be handled
• You have probably noticed that some exceptions are
popping up from time to time when you run your code
23
Some exceptions don’t need to be handled
Exception
IOException …
…
ArithmeticException NullPointerException
FileNotFoundException …
IndexOutOfBoundException IllegalArgumentException
24
Some exceptions don’t need be handled
• RuntimeExceptions are unchecked:
25
Some Exceptions must be handled
• Checked Exceptions must be handled
IOException …
…
ArithmeticException NullPointerException
FileNotFoundException …
IndexOutOfBoundException IllegalArgumentException
27
Checked Exception example
public static void methodX {
....
readFile(); //Might throw IOException
....
}//methodX
28
Checked Exception example
public static void methodX {
....
try {
readFile(); //Might throw IOException
} catch(IOException e) {
....
}
....
}//methodX
• Solution 1: Catch it
29
Checked Exception example
public static void methodX throws IOException {
....
readFile(); //Might throw IOException
....
}//methodX
• Solution 2: Throw it
30
Checked Exception example
public static void methodX throws IOException {
....
readFile(); //Might throw IOException
....
}//methodX
• Solution 2: Throw it
• If any method call or action inside this method causes an
IOException of any kind, it's thrown back to the method
that called this one.
31
Checked Exception example
public static void methodX throws IOException {
....
readFile(); //Might throw IOException
....
}//methodX
• Solution 2: Throw it
• If any method call or action inside this method causes an
IOException of any kind, it's thrown back to the method
that called this one.
• This method is immediately terminated at that point.
32
Checked Exception example
public static void methodX throws IOException {
....
readFile(); //Might throw IOException
....
}//methodX
• Solution 2: Throw it
• If any method call or action inside this method causes an
IOException of any kind, it's thrown back to the method
that called this one.
• This method is immediately terminated at that point.
• Now any other method that uses methodX must also
catch or throw IOExceptions.
33
Returning from a method - case 1
public static int methodX throws IOException {
....
readFile(); //No IOException is thrown this time
....
return 42;
}//methodX
34
Returning from a method - case 2
public static int methodX throws IOException {
....
readFile(); //IOException is thrown this time
....
return 42;
}//methodX
35
What throws what?
• You have to handle checked exceptions when using a
method that throws a checked exception
36
What throws what?
• You have to handle checked exceptions when using a
method that throws a checked exception
37
What throws what?
• We haven’t dealt with checked Exceptions yet in our
in-class examples, so let’s go back and look at a
method that throws an unchecked Exception
38
What throws what? RTFM
https://docs.oracle.co
• Search for “Java Integer class” online m/javase/8/docs/api/j
ava/lang/Integer.html
• Go to the “SE8” (or “SE7”) one
• Scroll down to the "parseInt()" description
• Right at the top you see
• "throws NumberFormatException". Aha!
39
What throws what? RTFM
https://docs.oracle.co
• Search for “Java Integer class” online m/javase/8/docs/api/j
ava/lang/Integer.html
• Go to the “SE8” (or “SE7”) one
• Scroll down to the "parseInt()" description
• Right at the top you see
• "throws NumberFormatException". Aha!
• What kind of Exception is that?
• Click on "NumberFormatException"
• You can immediately see that:
• It's a kind of "IllegalArgumentException"
• which is a kind of "RuntimeException"
• which is a kind of "Exception"
• Catching any of these will work.
40
Stupid try-catch tricks
• Technically, you can catch RuntimeExceptions:
Will print:
int[] mySmallArray = {1,2,3,4,5,6,7,8,9};
1
try{ 2
for (int i = 0; i < 1000; i++) 3
{ 4
System.out.println(mySmallArray[i]); 5
} 6
} 7
catch (ArrayIndexOutOfBoundsException e){ 8
9
System.out.println("End of the array");
End of the array
}
41
Stupid try-catch tricks
• Technically, you can catch RuntimeExceptions:
Will print:
int[] mySmallArray = {1,2,3,4,5,6,7,8,9};
1
try{ 2
for (int i = 0; i < 1000; i++) 3
{ 4
System.out.println(mySmallArray[i]); 5
} 6
} 7
catch (ArrayIndexOutOfBoundsException e){ 8
9
System.out.println("End of the array");
End of the array
}
42
Do not use try-catch as an if
• You’re not supposed to use try-catch to deal with
unchecked exceptions and emulate if statements
44
Do not use try-catch as an if
• Example:
• IOException is really java.io.IOException so
• Use import java.io.*;
• Or use import java.io.IOException;
46
File Input / Output
• It is pretty useful to be able to read or write to a file
• Allows you to analyse information stored in a file
• Allows you to print the results of an analysis in a
file
47
File Input / Output
• Java has multiple built-in classes for reading and
writing to a file
49
Stream
• Java reads or writes through “streams” of data
(either in bytes or characters)
Program File
50
Typical use
• For reading:
• Open a stream (building an object)
• Use the object’s methods to get data
• Close the stream (calling close() on the object)
• For writing:
• Open a stream (building an object)
• Use the object’s methods to write data
• Close the stream (calling close() on the object,
which also “flushes” the stream emptying it in
the file)
51
Some class examples for I/O
• I/O of characters:
Reader Writer
FileReader FileWriter
52
Some class examples for I/O
• I/O of bytes:
InputStream OutputStream
… …
FileInputStream FilterInputStream ObjectInputStream FileOutputStream FilterOutputStream ObjectOutputStream
DataInputStream
… DataOutputStream PrintStream
…
53
Some class examples for I/O
• I/O of bytes:
e.g. System.in
InputStream OutputStream
… …
FileInputStream FilterInputStream ObjectInputStream FileOutputStream FilterOutputStream ObjectOutputStream
DataInputStream
… DataOutputStream PrintStream
…
e.g. System.out
or System.err
54
Reading from files - low level
• FileReader is an example of a low-level, basic reader
• It’s not really meant to be used directly
• Will usually be “wrapped” inside another class
(another type of reader)
55
Reading from files - low level
• FileReader is an example of a low-level, basic reader
• It’s not really meant to be used directly
• Will usually be “wrapped” inside another class
(another type of reader)
57
Reading from files - mid level
• We'd need to write some methods to help us actually
use a FileReader… of course, someone has already done
that:
• A BufferedReader
• Handles the FileReader for us
• Provides more user-friendly methods
• Still not very fancy (won’t read an int or a double)
• Constructor:
• new BufferedReader(FileReader)
• This is known as “wrapping” a FileReader inside a
BufferedReader
58
BufferedReader methods
The fundamental methods:
• String readLine()
• Reads the next entire line from the file
• Returns null if there are no more lines
• int read()
• Reads one character from the file
• But returns its character code as an int, not a char.
• Cast it to (char) if you want to, but not if...
• It returns -1 for “end of file” (no more chars)
59
BufferedReader methods
The fundamental methods:
• void close()
• Release control of the file
• Should always be done after any file I/O of any type
• After the stream has been closed, calling methods
on the BufferedReader will throw an IOException
See FileReading2.java
60
Notes on file reading
• For any type of file I/O you need
• to use import java.io.*;
• to catch many kinds of IOExceptions
61
Notes on file reading
• BufferedReader will still only give you characters, or a
complete line
62
Advanced Scanner
• When a Scanner reads a line or String, it treats it as a
sequence of “tokens” (any consecutive non-blank
characters)
it sees it as
63
Advanced Scanner
• You can see if there are any more tokens, and, if so,
get the next one (as a String) with:
64
Advanced Scanner
• If you want to read tokens as int, long, float, double,
or boolean (but not char):
65
Advanced Scanner
• The nextLine( ) method is different
66
Another approach for parsing lines
• When you get a String from readLine(), sometimes it
can be useful to “split” the String
• Let’s say that your file has multiple rows (each line),
and items on each line (each column) is separated by
a specific character (e.g. a space, a dash, or a comma)
• Constructors:
• new FileWriter(String); //Give it the file name (the String)
• new FileWriter(File); //We'll cover "File" objects later
68
Writing to files - low level
• The low-level object is a FileWriter
• Also not really meant to be used directly
• Constructors:
• new FileWriter(String,true);
• new FileWriter(File,true);
69
Writing to files - high level
• A PrintWriter will control a FileWriter for you, and give
you the usual convenient methods:
70
Writing to files - high level
• A PrintWriter will control a FileWriter for you, and give
you the usual convenient methods:
• Methods:
• void print(..any type..)
• void println(..any type..)
• void printf(String, ...others...) //we'll discuss later
• void format(String, ...others...) //same as printf
• void close() //should always be done for files
71
Writing to files - high level
• A PrintWriter will control a FileWriter for you, and give
you the usual convenient methods:
• Methods:
• void print(..any type..)
• void println(..any type..)
• void printf(String, ...others...) //we'll discuss later
• void format(String, ...others...) //same as printf
• void close() //should always be done for files
Again, you must:
- Use import java.io.*;
- catch different types of IOExceptions See FileWriting.java
72
Extra material - File object
• A File object does not do any I/O
• Instead, it contains all of the information needed
to identify a particular file
• Its name, the "path" to find it, what disk it's on,
etc.
• Usually very system-dependent
73
Extra material - File Chooser
• A JFileChooser object can be used to
• give the user an ordinary file dialog box
• obtain a File object
• which can be used to create a FileReader,
FileWriter, etc.
• which in turn can be used to create a
BufferedReader or PrintWriter
See FileChoosingExample.java
74
File Chooser - advanced
• You can also add features to:
• Default to the current directory
• Restrict the choice to a particular type of file
• Handle the "Cancel" button properly
• Etc.
75
Cutting out the intermediary
• If you want to read non-character data (int, double,
etc.) from a text file, you can use a Scanner directly
76
Extra material: formatting
• Instead of print() or println() you can use printf() or
format() to control output exactly
77
Extra material: formatting
• Here's how to use them:
System.out.format("Casting %f to int gives %d %n",
23.8, (int)23.8 );
• The first parameter is a String that indicates exactly
how you want the data printed
• The red codes that start with % are where the data
goes
• Except %n which just gives a newline character
• There can be any number of other parameters
• These supply the actual data to print (in blue)
78
Formatting codes
• Commonly used codes:
• %d – print a decimal integer here (base 10 integer)
• %6d – use at least 6 characters to do that
• %f – print a floating-point value here
• %6f – use at least 6 characters to do that
• %6.2f – with exactly 2 of them after the decimal point
• %s – print a String here
• %n – print a newline (\n character) here
79
Formatting codes
• Previous style:
System.out.println(a+" plus "+b+" is "+(a+b)+".\n");
• Formatted style:
System.out.printf("%d plus %d is %d.%n",a,b,a+b);
• Most useful to
• Line up decimal points – perhaps use %7.2f
• Round off a number
• Use %4.1f to get 98.6 and not
98.59999999999999
80
Extra material: Binary files (bytes)
• We saw earlier that there are two types of file I/O:
• char (text) human readable with your favorite
text editor
• byte (binary) not human readable
81
Extra material: Binary files (bytes)
• If you do
int x=987654321; //A 4-byte number
outFile.print(x); //printing to a text file, using a PrintWriter (chars)
83
Extra material: Binary files (bytes)
• Methods for reading and writing:
• readInt(), readDouble(), readLong(), readUTF(), ...
• writeInt(x), writeDouble(x), writeLong(x),
writeUTF(x), ...
• UTF??
• That is a standard way to write and read String
data: 2 bytes for the length, then 1 or 2 bytes per
character
See BinaryIOSample.java
84
Extra material: Binary files (bytes)
• You can also use the ObjectOutputStream and
ObjectInputStream classes to write / read byte
versions of objects to / from a file
85