2013年12月18日星期三

Ask questions about exception handling

If my main method to call three methods in turn , f1 (), f2 (), f3 (). Three methods may throw an exception.
how to design exception handling can be achieved :
1. regardless of whether an exception is thrown three methods , can be executed in sequence f1, f2, f3.
2. If there is an exception in the main final play of all exception information .


------ Solution ------------------------------------ --------

feel so good as this simple :     
    

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());
   
}
         
  
is not to say, if the exception is caught and handled , the program is not terminated , will continue down the order of execution ?  
yes.
unless they use the throw statement in the catch block , the exception will be caught and then thrown out , otherwise dispose of captured , it will not terminate the program at the exception occurred .
------ For reference only -------------------------------------- -
give ideas, there are pre- flash people :
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());
   }
}

------ For reference only ----------------------------------- ----

feel so good as this simple :

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());
   
}

------ For reference only ----------------------------------- ----

feel so good as this simple :   
[/ code]   ah endorsed
------ For reference only ------------------------------- --------

feel so good as this simple :   
  

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());
   
}
 

is not to say, if the exception is caught and handled , the program is not terminated , will continue down the order of execution ?
------ For reference only -------------------------------------- -
floor China good IT, China good code
------ For reference only ---------------------------------------
2 Building on the good way .
------ For reference only -------------------------------------- -
when the exception is caught , the following code can proceed . Therefore, the second floor of the code is completely OK. Floor thinking too complicated.

The main exception is not done these small tests.

too lazy to write the code , on the second floor directly copy!
------ For reference only ---------------------------------------

feel so inferior so succinctly:   
  

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());
   
}
 

I noticed oh_Maxy mao brother, you are in the main inside out here , if not main inside, but a private void exe () inside, so it is easy to write the problem , right?
I think add a finally {} is better ...
------ For reference only ------------------ ---------------------

feel so good as this simple :     
    

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());
   
}
         
  
I noticed oh_Maxy mao brother, you are in the main inside out here , if not main inside, but a private void exe () inside, so it is easy to write the problem , right?   
I think add a finally {} is better ...  
not add finally, because each catch block has captured the exception , the equivalent internal digested .
finally dispensable .

没有评论:

发表评论