Catching exceptions
try {
int lo = 1;
int hi = 1;
int n = Integer.parseInt(args[0]);
System.out.println(lo);
for (int i = 1; i < n; i++) {
System.out.println(hi);
int temp = hi;
hi = lo + hi;
lo = temp;
}
} catch (ArrayIndexOutOfBoundsException e) {
System.err.println("No command line argument provided");
} catch (NumberFormatException e) {
System.err.println("Command line argument " + e.getMessage() + " is not an integer");
} finally {
System.out.println("Done!");
}
Specifying exceptions
public static int fibonacci(String[] args) throws ArrayIndexOutOfBoundsException, NumberFormatException {
int lo = 1;
int hi = 1;
int n = Integer.parseInt(args[0]);
for (int i = 1; i < n; i++) {
int temp = hi;
hi = lo + hi;
lo = temp;
}
return lo;
}