Lecture 14 - 02/17

Iteration and Exceptions

Exceptions

We throw exceptions explicitly with throw keyword, and provide useful information for user/for debugging. Example:

if (</*boolean check to throw the exception*/>) {
throw new RuntimeException("Error Message");
}

We can also try some code and catch any exceptions:

Iteration

import java.util.Iterator;
public class MyClass<T> extends Iterable<T> {
...
public class MyIterator implements Iterator<T> {
...
@Override
public boolean hasNext() {
...
}
@Override
public T next() {
...
}
}
@Override
public Iterator<T> iterator() {
return new MyIterator();
}
...
}