More ADTs and Exceptions
Daniel Archambault
CS-115: ADT and Exceptions
1
Previously in CS-115
Student s = …
int studentNumber = 42 String name
bool isRegistered = false
Remember the Class Variables!
”Rhys Williams”
CS-115: ADT and Exceptions
2
Previously in CS-115
How are variables of simple types represented?
CS-115: ADT and Exceptions
3
Previously in CS-115
How are variables of simple types represented? How are variables of class types represented?
CS-115: ADT and Exceptions
3
Previously in CS-115
How are variables of simple types represented? How are variables of class types represented? How does assignment work with simple types?
CS-115: ADT and Exceptions
3
Previously in CS-115
How are variables of simple types represented? How are variables of class types represented? How does assignment work with simple types? How does assignment work with class types?
CS-115: ADT and Exceptions
3
Previously in CS-115
How are variables of simple types represented? How are variables of class types represented? How does assignment work with simple types? How does assignment work with class types? What type do arrays behave like?
CS-115: ADT and Exceptions
3
Previously in CS-115
How are variables of simple types represented? How are variables of class types represented? How does assignment work with simple types? How does assignment work with class types? What type do arrays behave like?
What is the difference between a static and non-static attribute?
CS-115: ADT and Exceptions
3
Previously in CS-115
This lecture is only about ADTs
Error! Error! This lecture is also about exceptions
ADTs and Exceptions
CS-115: ADT and Exceptions
4
Abstract Data Type
Separates the behaviour of a data structure from its implementation
Detail how the module behaves
Describe the type of data stored
They should not supply implementation information
CS-115: ADT and Exceptions
5
Specification of ADT
Types of operations on data, typically:
Add data to the data
Remove data from the data
Query the data
Before representation design, think about operations Lead to better data representations
If contrained to read only the first element of the list…
CS-115: ADT and Exceptions
6
Data Abstraction and Problem Solving in Java, p 173
CS-115: ADT and Exceptions
7
Abstract Data Type vs Data Structure
ADTs and data structures are not the same thing Data structures
Specifies exactly how we store data
Specifies exact implementations of methods
These are composed of classes, attributes, and methods in Java
ADTs closer to design
Data structures close to implementation
CS-115: ADT and Exceptions
8
Attributes of List ADT
Items in a list have
A unique predecessor
A unique successor
Front does not have a predecessor End does not have a successor
Doesn’t tell you how it’s done. Just what is supported.
CS-115: ADT and Exceptions
9
Operations Supported by List ADT
Create an empty list
Determine whether a list is empty
Determine the number of items in a list
Add an item at a given position in the list
Remove the item at a given position in the list Remove all the items from the list
Retrieve (get) the item at a given position in the list
Doesn’t tell you how it’s done. Just what is supported.
CS-115: ADT and Exceptions
10
In Java
public Interface List{
public boolean isEmpty ();
public int numberOfElements ();
public void addItem (ListItem li, int pos); public ListItem getItem (int pos);
}
The closest thing to ADTs in Java are interfaces Specify methods without implementation
Specify constants
Not allowed attributes or any implementation
You can declare interfaces in Java (we will get to this)
CS-115: ADT and Exceptions
11
Exceptions
https://upload.wikimedia.org/wikipedia/commons/a/a8/Windows_XP_BSOD.png
Well, that wasn’t handled properly…
CS-115: ADT and Exceptions
12
Why Exception Handling?
Exception Handling is about handling errors.
To err is human…
It’s about anticipating very common errors
We aim to build build robust and fault tolerant programs.
Often associated with Input and Output (I/O), e.g., file I/O, user
input
More examples
⋆ out-of-bounds array index
⋆ arithmetic overflow (i.e. value outside range of values),
⋆ division by zero
Basically, recover now before things go really wrong
CS-115: ADT and Exceptions
13
Errors in Java
In Java, errors are thrown and caught
the code that caused the error throws and exception
⋆ the exception causes a immediate return out of the method the code that called the method has a catch clause
⋆ this code receives the error and handles it
The error itself is a class that inherits from throwable Throwable is a superclass of all errors in Java
CS-115: ADT and Exceptions
14
How to Handle Errors in Java
try {
//Do some risky method calls…
}
catch (IOException e1) {
//if an IOException recover here
}
catch (LemonException e2) {
//if Neal Harman threw an exception recover here
}
catch (Exception e3) {
//matches everything that inherits from exception
}
Appropriate catches called depending on error type
CS-115: ADT and Exceptions
15
What about finally?
try {
//Do some risky method calls…
}
….
finally {
//stuff happens here
}
No matter what happens, finally gets executed
any error, finally gets executed
no errors at all, finally gets executed
Finally usually releases resources, closes files/database connections, and network connections
CS-115: ADT and Exceptions
16
But I want to throw my own errors!
You can write code that doesn’t work… (not good) But you can also write code that throws good errors
in method definition declare method that throws and error
in the code, detect the error with if statements
use the throw keyword
CS-115: ADT and Exceptions
17
Example of Throwing Exceptions
int div (int a, int b) throws ArithmeticException {
if (b == 0)
throw new ArithmeticException (“Div by 0”);
else
return a/b;
}
Outside this method, you can catch the ArithmeticException
CS-115: ADT and Exceptions
18
Review
What is an ADT?
CS-115: ADT and Exceptions
19
Review
What is an ADT?
How does it differ from a data structure?
CS-115: ADT and Exceptions
19
Review
What is an ADT?
How does it differ from a data structure?
How do we handle code that could throw an error?
CS-115: ADT and Exceptions
19
Review
What is an ADT?
How does it differ from a data structure?
How do we handle code that could throw an error? How do we get a method to throw and error?
CS-115: ADT and Exceptions
19