Exceptions
throw
statement: throws an exception
1
2
3
4
5
6
|
public V get(K key) {
int location = findKey(key);
if (location < 0) { throw new IllegalArgumentException("Key " +
key + " does not exist in map."); }
return values[findKey(key)];
}
|
显式抛出异常
1
2
3
4
|
public static void main(String[] args) {
System.out.println("ayyy lmao");
throw new RuntimeException("For no reason.");
}
|
What has been Thrown, can be Caught
1
2
3
4
5
6
7
8
9
10
11
|
Dog d = new Dog("Lucy", "Retriever", 80);
d.becomeAngry();
try {
d.receivePat();
} catch (Exception e) {
System.out.println(
"Tried to pat: " + e);
}
System.out.println(d);
|
由callstack顺序
exception是一种对象,有时可能见到的错误↓
最好明确怎么处理exception

1
2
3
|
public static void gulgate() throws IOException {
... throw new IOException("hi"); ...
}
|
有时需要考虑main情况
上面没有明确处理
checked与否见种类

Iteration
创建能支持for (Item i : someIterable)
的情况
The Iterable Interface
1
2
3
|
public interface Iterable<T> {
Iterator<T> iterator();
}
|
1
2
3
4
5
|
package java.util;
public interface Iterator<T> {
boolean hasNext();
T next();
}
|
假如要遍历ArrayMap,需要对key进行操作
到此完成,可以执行遍历