Professional Documents
Culture Documents
Packages
Exceptions
Documentation
Packages
import java.awt.Point;
class MyClass {
public static void main (String args[]) {
Point p = new Point();
}
}
Equally we can import a whole package if we want to use several
Classes in it…
import java.awt.*;
No major overhead – just directs compiler to find the files, doesn’t put
them all in your program.
javac us\gov\presidents\Obama.java
or
javac us\gov\presidents\*.java
to compile everything.
javac unpackaged/lazyCode/*.java
java -classpath unpackaged/lazyCode/;. MyClass
java.util.Vector
java.util.ArrayList
java.util.Stack
java.util.Hashtable
java.util.Properties
java.util.Bitset
java.util.Enumeration
Objects
Vector()
Vector(int size)
Vector(int size, int increment)
addElement(Object ob)
insertElementAt(Object ob, int index)
Add elements in at the top or index position.
elementAt(int index)
Gets an element at the index position.
contains(Object ob)
Checks the object is in the Vector.
Example
import java.util.*;
Packages
Exceptions
Documentation
Problems
Fine and dandy, but what if our code causes their code to
break horribly?
FileReader f = new FileReader(someFileThatDoesn’tExist);
For example, if you try and add something to an array outside its size
the JVM makes an object of type…
ArrayIndexOutOfBoundsException
Exceptions
try {
FileWriter f = new FileWriter(someFile);
} catch (Exception e) {
return “File not found”;
}
f.write(“Hello World”);
FileWriter f = null;
try {
f = new FileWriter(someFile);
} catch (Exception e) {
return “File not found”;
}
f.write(“Hello World”);
The code stops at the problem point and jumps to where the
method was called, and the exception object gets passed to that
point.
This continues until the main method is reached if it’s not handled.
The default handler then handles the problem.
Usually the program breaks and a ‘stack trace’ is printed. This lists all
the methods the problem has propagated through.
public class RubbishMaths {
}
Nesting catches
Note that the relationship between subclasses and their
parent class means that we can nest catches…
try {
try {
// Operation
} catch (RuntimeException rte) {
// Do something with rte.
}
} catch (Exception e) {
// Do something with e if it’s not a RuntimeException.
}
Laddered catches
try {
// Operation
} catch (OurException oe) {
// Do something with oe
} catch (Exception e) {
// Do something with e if it’s not of type //
OurException.
}
What to do with Exceptions
All exceptions except RuntimeExceptions, must be caught.
The usual thing in debugging is to print the error and the stack trace.
e.printStackTrace();
Can also write its name to a standard error log…
System.err.println(e);
…but this is usually the screen anyhow.
In a real program we should do something about the error so the
user doesn’t get a message.
Finally
try {
// Operation.
} catch (Exception e) {
// Do something with e.
} finally {
// Do something important.
}
Making Exceptions
The also allows us to force people who use our classes to deal
with problems we know may arise.
try {
// whatever
} catch (ExceptionType label) {
// fix stuff
}
This lecture
Packages
Exceptions
Documentation
The Java Packages
public Write() {
FileWriter f = null;
try {
f = new FileWriter (someFile);
} catch (IOException ioe) {
ioe.printStackTrace();
}
f.write(“Hello World”);
f.close();
}
public static void main (String args[]) {
new Write();
}
}
Some of the main packages
// End of Class.
}
Class Vector<E>
add(E e)
Or
containsAll(Collection<?> c)
addAll(Collection<? extends E> c)
These mean the class stores java.lang.Object type objects, but can
be forced to be more specific to reduce potential casting errors.
Example
These storage objects can be parameterized. i.e. they can be given a
specific type other than Object.
Vector<String> v = new Vector<String>();
You can build your own parameterizable types that take in Classes of
a particular type…