Common run-time exceptions
Also called unchecked exceptions, exception names.
These are unchecked: javac compiles code that may throw them. Read the message, then the first at Main.… line in your own file.
| Exception | Thrown by |
|---|
ArithmeticException | an int divided by zero |
ArrayIndexOutOfBoundsException | a[5] on an array of length 5 |
NullPointerException | a method called through a null variable |
NumberFormatException | Integer.parseInt("3.5") |
InputMismatchException | nextInt() reading abc |
ClassCastException | a cast to a class the object isn't |
Careful: The line a trace names is where the problem was noticed. The bad value may come from a caller further down the trace.
Taught in Exceptions and stack traces
try and catch
Also called catching an exception, catch block.
Lines in the try run until one throws; the rest of the try is skipped, the matching catch runs, and the program carries on below it.
try {
int v = Integer.parseInt(s);
total += v;
} catch (NumberFormatException e) {
System.out.println("skip " + s);
}
Careful: Control never goes back into the try. A try around a whole loop ends the loop at the first exception; put it inside to skip one item. Catch the specific type, not Exception.
Taught in try and catch
Checked exceptions and throws
Also called FileNotFoundException, unreported exception, throws clause.
A checked exception must be caught or declared with throws, or the code doesn't compile: unreported exception FileNotFoundException; must be caught or declared to be thrown.
public static void main(String[] args)
throws FileNotFoundException {
Careful: throws doesn't handle the exception: if the file is missing at run time, the program still stops with a trace. Each method that may throw it must catch or declare it.
Taught in Checked exceptions and throws
Throwing an exception
Also called throw, IllegalArgumentException, precondition check.
throw makes an exception and throws it at once. The method stops, and the exception passes back through the callers until a catch handles it.
if (x < 0) {
throw new IllegalArgumentException(
"negative: " + x);
}
Careful: throw is a statement in a body; throws goes in a header. Returning −1 instead lets a caller use a wrong value without noticing.
Taught in Throwing an exception
Reading a text file line by line
Also called Scanner and File, split, hasNextLine.
hasNextLine() is true while a line is left; nextLine() returns it without the line break; split(",") gives the fields as Strings.
Scanner in = new Scanner(
new File("scores.txt"));
while (in.hasNextLine()) {
String line = in.nextLine();
String[] p = line.split(",");
}
Careful: Fields are Strings until Integer.parseInt reads them. split keeps empty fields, and a line with no comma has no p[1].
Taught in Reading a text file
Base case and recursive case
Also called recursion, StackOverflowError.
The base case is answered without a call; every other call works on a smaller input that moves toward it. Each call has its own parameters.
static int sumTo(int n) {
if (n == 0) {
return 0;
}
return n + sumTo(n - 1);
}
Careful: A base case that is missing or never reached (an odd n stepping by 2 past 0) fills the stack: StackOverflowError.
Taught in Recursion: a method that calls itself
Work before and after the recursive call
Also called order of output in recursion, call tree.
A line before the recursive call runs on the way down, in call order. A line after it waits until the call returns, so it runs on the way back up, reversed: up(3) that prints after its call prints 1, 2, 3.
Careful: Count calls with the call tree, not the depth: fib(n) makes two calls each time, and fib(10) makes 177 calls.
Taught in Tracing recursion
Merge sort
Also called split and merge.
Split the array in half, sort each half the same way, and merge the two sorted halves by taking the smaller front number each time. A piece of one number is sorted already.
Careful: The halving takes about log₂ n levels (10 for 1,024 numbers), not n levels.
Taught in Recursion on strings and arrays