C209 6P P05 Session1
C209 6P P05 Session1
C209 6P P05 Session1
C209
ADVANCED OBJECT-ORIENTED PROGRAMMING
PROBLEM 05
SESSION 1
Sushi World
EXCEPTIONS
2
What is an Exception?
Definition
An exception is an event, which occurs during the
execution of a program, that disrupts the normal flow
of the program's instructions.
Unhandled
exception
P05 Sushi World 5
Handling Exceptions
In Java, to handle exceptions, we use the try-catch
block to help us:
try
{
// some code that may cause an error
// (throw an exception)
…
}
catch(<ExceptionType> <nameOfException>)
{
// some code to respond to the error
// (Handle the exception)
…
}
int a = 3, b = 0;
ArithmeticException int q = a / b;
System.out.println(Integer.parseInt("ten"));
NumberFormatException
String s = "Hello";
StringIndexOutOfBoundsException System.out.println(s.charAt(5));
Exception ClassCastException
IOException NullPointerException
… NumberFormatException
RuntimeException StringIndexOutOfBoundsException
…
P05 Sushi World 9
Exercise
Open ArithmeticExceptionExample.java
a) Which line of code in ArithmeticExceptionExample
caused the error highlighted below? What is the
reason?
Execute NullPointerExceptionExample.java
There are no syntax errors in the code. However, as the loop attempts
to perform an operation on every single element in the array, when it
hits a null, an error will be thrown.
P05 Sushi World File name and line number of the code where the error ocurred. 14
Exercise
Modify the code in NullPointerExceptionExample so that it looks
like the below:
Save Helper.java
Execute NumberFormatTest.java again. What is the
output? Explain why this happened.
P05 Sushi World 22
Exercise - solution
The try-catch was there to help handle number
format exceptions such as when the user enters an
alphabet.
Save Helper.java
26
Simple file reading
Related classes to read information from a file
File
To create a new File instance from a file path.
FileReader
To read from the specified file.
Reads streams of characters.
BufferedReader
To read text from a character-input stream.
Buffers characters so as to provide for the efficient reading of characters,
arrays, and lines.
} catch (FileNotFoundException e) {
Try/catch guards
e.printStackTrace();
against non-existent
} catch (IOException io) {
file opening
io.printStackTrace();
}
P05 Sushi World 29
Performing File I/O - Reading
We can also combine the lines to prepare the file,
into a single line. This is seen in some tutorials online
File file = new File("data.txt");
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
33
File writing
Related classes to write information to a file
File
To create a new File instance from a file path.
FileWriter
To write to the specified file.
If the file does not exist yet, the file will be created.
If the file already exists, we can either overwrite the file, or append to the file.
Reads streams of characters.
BufferedWriter
To write text to a character-output stream
Buffers characters so as to provide for the efficient writing of single characters,
arrays, and strings.
Try/catch guards
against error on file
operations
Try/catch guards
against error on file
operations
to:
bw.write("This is line 3\n");
bw.write("This is line 4\n");
44
Convert String to Numbers, Numbers to String
Whenever we read data from a text file, the information
is typically read as a String value.
int x = 10;
Or
What type of exception do you think will be thrown for the last instruction?