自定义异常类:
public class CustomException extends Exception{
public CustomException(){}
public CustomException(String message){
super(message);
}
// public CustomException(param ..){...}
}
main():
try{
try{
Class<?> clazz = Class.forName(className);
Method method = Clazz.getDeclaredMethod(Param);
method.invoke();
}catch(InvocationTargetException ext){
try{
throw ext.getTargetException();
}catch(CustomException cusEx){
throw cusEx;
}catch(Throwable e){
// TODO
}
}
}catch(CustomException ex){
// TODO
}catch(Exception e){
// TODO
}
public class CustomException extends Exception{
public CustomException(){}
public CustomException(String message){
super(message);
}
// public CustomException(param ..){...}
}
main():
try{
try{
Class<?> clazz = Class.forName(className);
Method method = Clazz.getDeclaredMethod(Param);
method.invoke();
}catch(InvocationTargetException ext){
try{
throw ext.getTargetException();
}catch(CustomException cusEx){
throw cusEx;
}catch(Throwable e){
// TODO
}
}
}catch(CustomException ex){
// TODO
}catch(Exception e){
// TODO
}
本文介绍了一种自定义异常类的设计方法,并展示了如何在Java中使用该自定义异常进行错误处理。通过具体的代码示例,文章详细解释了如何创建自定义异常类以及如何在程序的不同层级捕获并抛出自定义异常。
996

被折叠的 条评论
为什么被折叠?



