日期:2014-05-20  浏览次数:20613 次

在定义方法中抛出多个异常,想在main中怎么一一catch处理?
如题,想在main方法最后添加一段异常处理,将各个异常对应的内容一一写入log日志中
如果直接catch的话会报错,因为log日志中又会引用上面各个方法中定义的参数,又不能在方法中catch异常直接写日志
main中该如何写呢?

------解决方案--------------------
public static void someMethod() throws Exception {

throw new Exception("abc");//实际上这里就把日志内容做好就可以了
}
public static void main(String[] args) {
try{
someMethod();//你的操作
}catch(Exception e){//处理异常
System.out.println(e.getMessage());//这里直接把内容打印到日志
}
}