动手动脑

1、动手动脑:

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

class AboutException {

   public static void main(String[] a)

   {

      int i=0, j=1, 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");

     }

  }

}

当j=0时出现异常,所以捕获错误,catch处理异常,不管是否有异常发生,finally语句块中的语句始终保证被执行。如果没有提供合适的异常处理代码,JVM将会结束掉整个应用程序。

Throwable类有两个直接子类:

Exception:出现的问题是可以被捕获的;

Error:系统错误,通常由JVM处理。

可捕获的异常又可以分为两类:

(1)Check异常:直接派生自Exception的异常类,必须被捕获或再次声明抛出

(2)Runtime异常:派生自RuntimeException的异常类。使用throw语句可以随时抛出这种异常对象:

   throw new ArithmeticException(…);

2、动手动脑:多层的异常捕获-1

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

public class CatchWho {

    public static void main(String[] args) {

        try {

             try {

                 throw new ArrayIndexOutOfBoundsException();

             }

             catch(ArrayIndexOutOfBoundsException e) {

                System.out.println(  "ArrayIndexOutOfBoundsException" +  "/内层try-catch");

             }

            throw new ArithmeticException();

        }

        catch(ArithmeticException e) {

            System.out.println("发生ArithmeticException");

        }

        catch(ArrayIndexOutOfBoundsException e) {

           System.out.println(  "ArrayIndexOutOfBoundsException" + "/外层try-catch");

        }

    }

}

结果:ArrayIndexOutOfBoundsException/内层try-catch

发生ArithmeticException

动手动脑:多层的异常捕获-2

public class CatchWho2 {

    public static void main(String[] args) {

        try {

             try {

                 throw new ArrayIndexOutOfBoundsException();

             }

             catch(ArithmeticException e) {

                 System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch");

             }

            throw new ArithmeticException();

        }

        catch(ArithmeticException e) {

            System.out.println("发生ArithmeticException");

        }

        catch(ArrayIndexOutOfBoundsException e) {

            System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch");

        }

    }

}

结果:ArrayIndexOutOfBoundsException/外层try-catch

3、动手动脑

结果:in Level 1

in Level 2

in Level 3

Level 3:class java.lang.ArithmeticException

In Level 3 finally

Level 2:class java.lang.ArithmeticException

In Level 2 finally

In Level 1 finally

当有多层嵌套的finally时,异常在不同的层次抛出,在不同的位置抛出,可能会导致不同的finally语句块执行顺序。

4、动手动脑

finally语句块一定会执行吗?

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

        

}

    

}

}

结果:in main

Exception is thrown in main

总结:当存在try中有throw new Exception(...)finally不会执行。

编写一个程序,此程序在运行时要求用户输入一个 整数,代表某门课的考试成绩,程序接着给出“不及格”、“及格”、“中”、“良”、“优”的结论。
要求程序必须具备足够的健壮性,不管用户输入什 么样的内容,都不会崩溃。


package score;
//信1605-2 刘宏琦 20163428 2017.11.16
import java.util.*;
public class Score{
int score;
void Setscore() {

Scanner in=new Scanner(System.in);
System.out.println("请输入成绩:");
try{ //try输入
score=in.nextInt(); //输入成绩


if(score<=100&&score>=90)
{
System.out.println("优");
}
if(score<90&&score>=80)
{
System.out.println("良");
}
else if(score<80&&score>=70)
{
System.out.println("中");
}
else if(score<70&&score>=60)
{
System.out.println("及格");
}
else if(score<60&&score>=0)
{
System.out.println("不及格");
}
else if(score<0||score>100)
{
System.out.println("超出范围!!");
Setscore();
}

}
catch (Exception e) //捕获异常
{
System.out.println("错误输入!! ");
Setscore(); //返回输入
}

finally //结束

{

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

}

}

public static void main(String[] args) {
// TODO Auto-generated method stub
Score s=new Score();
s.Setscore();
}
}

 

原文地址:https://www.cnblogs.com/bangandwolf/p/7845596.html