09异常处理

动手动脑一:

         代码:

                   import javax.swing.*;

class AboutException {

               public static void main(String[] a)

                          {

            int i=1, j=0, k;//double可以被0整除,整数和浮点型运算不一样

      k=i/j;

         try

         {

                  

                   k = i/j;    // Causes division-by-zero exception

                   //throw new Exception("Hello.Exception!");

         }

        

         catch ( ArithmeticException e)

         {

                   System.out.println("被0除.  "+ e.getMessage());

         }

        

         catch (Exception e)

         {

                   if (e instanceof ArithmeticException)

                            System.out.println("被0除");

                   else

                   { 

                            System.out.println(e.getMessage());

                           

                   }

         }

        

         finally

     {

                     JOptionPane.showConfirmDialog(null,"OK"+k);

     }

                  

  }

}

结论:

Try{

                            //可能发生运行错误的代码;

                   }

                   catch(异常类型     异常对象引用){

                            //用于处理异常的代码

                   }

                   finally{

                            //用于“善后” 的代码

                   }

动手动脑二:

截图:

动手动脑三:

截图:

动手动脑四:

代码:

public class EmbededFinally {

 

   

         public static void main(String args[]) {

       

                   int result;

       

                   try {

           

                            System.out.println("in Level 1");

 

          

                           try {

               

                                     System.out.println("in Level 2");

  // result=100/0;  //Level 2

              

                                    try {

                  

                                             System.out.println("in Level 3");

                     

                                             result=100/0;  //Level 3

               

                                     }

               

                                     catch (Exception e) {

                   

                                               System.out.println("Level 3:" + e.getClass().toString());

               

                                     }

               

               

                                     finally {

                   

                                               System.out.println("In Level 3 finally");

               

                                     }

                

              

                                     // result=100/0;  //Level 2

 

           

                                     }

           

                            catch (Exception e) {

              

                                    System.out.println("Level 2:" + e.getClass().toString());

          

                           }

                           finally {

               

                                     System.out.println("In Level 2 finally");

          

                             }

            

                            // result = 100 / 0;  //level 1

       

                   }

       

                   catch (Exception e) {

           

                            System.out.println("Level 1:" + e.getClass().toString());

       

                   }

       

                   finally {

          

                 System.out.println("In Level 1 finally");

       

                   }

   

         }

 

}

截图:

总结:在一个方法体中finally执行的是相对应的catch的内容

动手动脑五:

代码:

 

public class SystemExitAndFinally {

 

   

         public static void main(String[] args)

    {

       

                   try{

 

           

                            System.out.println("in main");

           

                            throw new Exception("Exception is thrown in main");

 

                               //System.exit(0);

 

       

                   }

       

                   catch(Exception e)

 

                 {

           

                            System.out.println(e.getMessage());

           

                            System.exit(0);

       

                   }

       

                   finally

       

                   {

           

                            System.out.println("in finally");

       

                   }

         }

}

截图:

总结:当catch中有exit退出程序时,这时就不在执行finally

原文地址:https://www.cnblogs.com/shouhutian/p/6102832.html