为什么我在尝试抛出它们时需要用try
/包装我抛出的自定义异常catch
,但我不必为通用异常这样做?就像在示例中一样,我的Exception
子类:
public class MyException extends Exception {
public MyException(String msg) {
super(msg);
}
}
抛出异常:
public class Exe {
private static void testex(String test) {
if (null!=test) {
throw new UnsupportedAddressTypeException();
} else {//Removing try/catch block results in compile failure
try {
throw new MyException("message");
} catch (MyException e) {
e.printStackTrace();
}
}
}
}