动手动脑

动手动脑1

程序运行结果:

ArrayIndexOutOfBoundsException/内层try-catch发生ArithmticException

动手动脑2

运行结果:

ArrayIndexOutOfBoundsException/外层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);

 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");

        

        }

    

    }

 

结果:

in Level 1

in Level 2

in Level 3

Level 3:class java.lang.ArithmeticException

In Level 3 finally

In Level 2 finally

In Level 1 finally

 import javax.swing.*;

public class ChengJi0 {

       public static void main(String[] args)

       {

       int a=0;

        

       

      

        

        try{

        String ing = JOptionPane.showInputDialog("输入你的成绩");

            

        a = Integer.parseInt(ing); 

            

        }

        catch(Exception e)

    {

        JOptionPane.showMessageDialog(null,"输入不正确,请重新输入","学生成绩",javax.swing.JOptionPane.INFORMATION_MESSAGE);

    System.exit(0);

       

        }

        if(a>=0&&a<60)

        {

        JOptionPane.showMessageDialog(null,"不及格","学生成绩",javax.swing.JOptionPane.INFORMATION_MESSAGE);

        }

        

        if(a>=60&&a<70)

        {

        JOptionPane.showMessageDialog(null,"及格","学生成绩",javax.swing.JOptionPane.INFORMATION_MESSAGE);

        

        }

        if(a>=70&&a<80)

        {

        JOptionPane.showMessageDialog(null,"","学生成绩",javax.swing.JOptionPane.INFORMATION_MESSAGE);

        

        }

        if(a>=80&&a<90)

        {

        JOptionPane.showMessageDialog(null,"","学生成绩",javax.swing.JOptionPane.INFORMATION_MESSAGE);

        

        }

        

         if(a>=90&&a<=100)

        {

        JOptionPane.showMessageDialog(null,"","学生成绩",javax.swing.JOptionPane.INFORMATION_MESSAGE);

        }

        if(a>100||a<0)

        {

        JOptionPane.showMessageDialog(null,"输入不正确","学生成绩",javax.swing.JOptionPane.INFORMATION_MESSAGE);

            

        

        }

        

  

            

            

    }  

        }

       

 

原文地址:https://www.cnblogs.com/lipengpengpeng/p/4966275.html