日期:2014-05-20 浏览次数:20704 次
Quote: 引用: Quote: 引用: 先给个思路,有事先闪人: public static int flagIndex = 0; public static StringBuffer errMsg = ""; public static void main(String[] args){ try{ f1(); flagIndex = 1; f2(); flagIndex = 2; f3(); }catch(Exception ex){ errMsg.append(ex.getMessage()); if(flagIndex == 0){ try{ f2(); flagIndex = 3; f3(); }catch(Exception exx){ errMsg.append(exx.getMessage()); if(flagIndex==3){ try{ f3(); }catch(Exception exxxx){ errMsg.append(exxxx.getMessage()); } } } }else if(flagIndex == 1){ //获取方法f2执行出现的异常信息 errMsg.append(ex.getMessage()); try{ f3(); }catch(Exception exxx){ errMsg.append(exxx.getMessage()); } }else{ //获取方法f3执行出现的异常信息 errMsg.append(ex.getMessage()); } }finally{ System.out.println(errMsg.toString()); } } 感觉这样不如这样简洁: public static void main(String[] args){ StringBuilder errMsg = new StringBuilder(); try{ f1(); }catch(Exception ex){ errMsg.append("f1()的异常信息:").append(ex.getMessage()); } try{ f2(); }catch(Exception exx){ errMsg.append("f2()的异常信息:").append(exx.getMessage()); } try{ f3(); }catch(Exception exxx){ errMsg.append("f3()的异常信息:").append(exxx.getMessage()); } // 打印异常 System.out.println(errMsg.toString()); } 是不是就是说如果异常被捕获并且处理了,程序就不会终止,会继续往下顺序执行?
Quote: 引用: 先给个思路,有事先闪人: public static int flagIndex = 0; public static StringBuffer errMsg = ""; public static void main(String[] args){ try{ f1(); flagIndex = 1; f2(); flagIndex = 2; f3(); }catch(Exception ex){ errMsg.append(ex.getMessage()); if(flagIndex == 0){ try{ f2(); flagIndex = 3; f3(); }catch(Exception exx){ errMsg.append(exx.getMessage()); if(flagIndex==3){ try{ f3(); }catch(Exception exxxx){ errMsg.append(exxxx.getMessage()); } } } }else if(flagIndex == 1){ //获取方法f2执行出现的异常信息 errMsg.append(ex.getMessage()); try{ f3(); }catch(Exception exxx){ errMsg.append(exxx.getMessage()); } }else{ //获取方法f3执行出现的异常信息 errMsg.append(ex.getMessage()); } }finally{ System.out.println(errMsg.toString()); } } 感觉这样不如这样简洁: public static void main(String[] args){ StringBuilder errMsg = new StringBuilder(); try{ f1(); }catch(Exception ex){ errMsg.append("f1()的异常信息:").append(ex.getMessage()); } try{ f2(); }catch(Exception exx){ errMsg.append("f2()的异常信息:").append(exx.getMessage()); } try{ f3(); }catch(Exception exxx){ errMsg.append("f3()的异常信息:").append(exxx.getMessage()); } // 打印异常 System.out.println(errMsg.toString()); }
先给个思路,有事先闪人: public static int flagIndex = 0; public static StringBuffer errMsg = ""; public static void main(String[] args){ try{ f1(); flagIndex = 1; f2(); flagIndex = 2; f3(); }catch(Exception ex){ errMsg.append(ex.getMessage()); if(flagIndex == 0){ try{ f2(); flagIndex = 3; f3(); }catch(Exception exx){ errMsg.append(exx.getMessage()); if(flagIndex==3){ try{ f3(); }catch(Exception exxxx){ errMsg.append(exxxx.getMessage()); } } } }else if(flagIndex == 1){ //获取方法f2执行出现的异常信息 errMsg.append(ex.getMessage()); try{ f3(); }catch(Exception exxx){ errMsg.append(exxx.getMessage()); } }else{ //获取方法f3执行出现的异常信息 errMsg.append(ex.getMessage()); } }finally{ System.out.println(errMsg.toString()); } }
public static int flagIndex = 0; public static StringBuffer errMsg = ""; public static void main(String[] args){ try{ f1(); flagIndex = 1; f2(); flagIndex = 2; f3(); }catch(Exception ex){ errMsg.append(ex.getMessage()); if(flagIndex == 0){ try{ f2(); flagIndex = 3; f3(); }catch(Exception exx){ errMsg.append(exx.getMessage()); if(flagIndex==3){ try{ f3(); }catch(Exception exxxx){ errMsg.append(exxxx.getMessage()); } } } }else if(flagIndex == 1){ //获取方法f2执行出现的异常信息 errMsg.append(ex.getMessage()); try{ f3(); }catch(Exception exxx){ errMsg.append(exxx.getMessage()); } }else{ //获取方法f3执行出现的异常信息 errMsg.append(ex.getMessage()); } }finally{ System.out.println(errMsg.toString()); } }
public static void main(String[] args){ StringBuilder errMsg = new StringBuilder(); try{ f1(); }catch(Exception ex){ errMsg.append("f1()的异常信息:").append(ex.getMessage()); } try{ f2(); }catch(Exception exx){ errMsg.append("f2()的异常信息:").append(exx.getMessage()); } try{ f3(); }catch(Exception exxx){ errMsg.append("f3()的异常信息:").append(exxx.getMessage()); } // 打印异常 System.out.println(errMsg.toString()); }