在上述 main 方法中,最後子句捕捉到泛用 java.lang.Exception。請改為
建立專用的 catch 子句,來處理從 try 子句擲出的每一個宣告的異常。
附註:為確保柔性結束,有些狀況較適合泛用 catch。一般而言,
越具體的 catch 子句,可以越明確並產生更大控制權的選項。
public static class Loader {
public void load() throws UnsupportedOperationException, ClassNotFoundException {
}
}
public static void main(String[] args) {
Loader loader = new Loader();
try {
loader.load();
} catch ( UnsupportedOperationException e1 ) {
System.out.println( "load is not implemented" );
} catch ( ClassNotFoundException e2 ) {
System.out.println( "No class " + e2.getMessage() );
}
}
|
|