java 动手动脑

.请阅读并运行AboutException.java示例,然后通过后面的几页PPT了解Java中实现异常处理的基础知识。

1.源代码:

 

import javax.swing.*;

class AboutException {

   public static void main(String[] a)

   {

      int  i=1, j=0, k;

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

     }

  }

}

2.Java中实现异常处理的基础知识。

异常处理的目的是依据实际情况提供不同的错误应对策略与手段,使程序更稳定,更安全。

异常处理的主要用途是提供准确的错误消息,解释失败的原因、位置和错误类型等,同时提供一定的恢复能力,尽可能地保证数据完整性不被破坏,并让程序能继续运行。

.阅读以下代码(CatchWho.java),写出程序运行结果:

 

 

运行结果:

 

.写出CatchWho2.java程序运行的结果

 程序代码:

 

运行结果:

 

.当有多个嵌套的try…catch…finally时,要特别注意finally的执行时机。

请先阅读 EmbedFinally.java示例,再运行它,观察其输出并进行总结。

特别注意:

当有多层嵌套的finally时,异常在不同的层次抛出 ,在不同的位置抛出,可能会导致不同的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应用:

*资源泄露:当一个资源不再被某应用程序使用,但此程序并未向系统声明不再使用此资源时发生这种情况

*finally语句块主要用于解决资源泄露问题,它位于catch语句块之后,JVM保证它们一定执行。

*注意:finally语句块中也可能发生异常,如果这种情况发生,先前的异常被放弃。

五:辨析:finally语句块一定会执行吗?

请通过 SystemExitAndFinally.java示例程序回答上述问题

源代码:

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

        

}

    

}

}

截图:

 

辨析:finally语句块一定会执行吗?

会执行的。

 

六.编写一个程序,此程序在运行时要求用户输入一个 整数,代表某门课的考试成绩,程序接着给出“不及格”、“及格”、“中”、“良”、“优”的结论。

要求程序必须具备足够的健壮性,不管用户输入什 么样的内容,都不会崩溃。

1.源代码:

package tyr;

import java.util.*;

class AException extends Exception

{

String a;

AException()  

{

a="输入有误";   

}

public String toString()

{

return a;

}

}

class A

{

    public static void main(String args[])

    {

          while(1>0)

    {

     Scanner sc = new Scanner(System.in);

     System.out.println("请输入考试成绩(0~100):");

        try 

        {

            String s = sc.nextLine();     

            getnum(s);

        }

        catch (AException e)

        {

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

        }

       }

    }

    private static void getnum(String s) throws AException

    {

         for (int i = s.length()-1; i >= 0;i--)

        {

         int chr = s.charAt(i);

            if (chr < 48 || chr > 57)

            {

             throw new AException();

            }

        }  

     double num = Double.parseDouble(s);   

        if (num < 0 || num> 100)

        {

            throw new AException();

        }

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

        {

            System.out.print("不及格 ");

        }

        else if (num >= 60 && num <= 70)

        {

            System.out.print("及格 ");

        }

        else if (num>= 70 && num<= 80)

        {

           System.out.print(" ");

        }

        else if (num >= 80 && num <= 90)

        {

            System.out.print(" ");

        }

        else 

        {

            System.out.print(" ");

        }

    }

}

2.结果:

 

原文地址:https://www.cnblogs.com/gong123/p/6101149.html