java中09的动手动脑

Java09-异常处理中的动手动脑

一:异常处理的基础知识

代码:

package Shi;

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

     }

 

  }

}

截图:

 

分析-异常处理的基础知识:

1、 把可能会发生错误的代码放进try语句块中。

2、 当程序检测到出现了一个错误时会抛出一个异常对象。异常处理代码会捕获并处理这个错误。

3、 当异常发生时,程序控制流程由try语句块跳转到catch语句块。

4、 不管是否有异常发生,finally语句块中的语句始终保证被执行。

5、 如果没有提供合适的异常处理代码,JVM将会结束掉整个应用程序。

修改:如果将j=0修改,并且将throw前面的注释去掉,就会运行捕获异常后的程序。

截图:

 

二:浮点型

如果把类型改成double就会正常运行了

原因是double类型的变量在做除法运算时,时允许除数为零的。

 

三:多层的异常捕获

代码:

package Shi;

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

        }

    }

}

截图:

 

分析:此程序运行到外部的try时,继续运行内部的try,抛出ArrayIndexOutOfBoundsException,紧接着还是在外部try中便捕获ArrayIndexOutOfBoundsException,输出“ArrayIndexOutOfBoundsException" +  "/内层try-catch”,然后又抛出ArithmeticExceptiontry中的语句运行完成,由于ArrayIndexOutOfBoundsException已经被捕获过了,所以只运行捕获ArithmeticException的部分,输出“发生ArithmeticException”,所以有截图中的运行结果。

代码:

package Shi;

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

        }

    }

}

截图:

 

分析:在此程序中,外部try抛出ArrayIndexOutOfBoundsException后,直接运行捕获ArrayIndexOutOfBoundsException地方的程序,中间的程序没有运行,所以没有抛出ArithmeticException,所以只输出“ArrayIndexOutOfBoundsException" + "/外层try-catch”。

四:多个嵌套的try   catch   finally

代码:

package Shi;

 

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

        

}

    

}

 

}

截图:

 

分析:正常运行try嵌套并输出前三行结果,在第三个try中,输出后抛出了int除数为零的异常,紧接着将会寻找第一个捕获异常的地方,继续运行,否则将只运行finally中的程序。所以最后有如图的输出情况。

五:finally语句

代码:

package Shi;

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中有语句:“System.exit(0)”,强行退出了程序,所以没有执行finally中的语句。

六:考试成绩的程序

代码:

package Shi;

import java.util.Scanner;

class MyException extends Exception{

public MyException(String msg){

super(msg);

}

}

 

public class Shi {

 

public static void main(String[] args) {

System.out.println("输入成绩(正整数)");

Scanner input=new Scanner(System.in);

boolean a3=true;

while(a3){

String a1=input.next();

char a[]=new char[a1.length()];

for(int i=0;i<a1.length();i++){

a[i]=a1.charAt(i);

}

boolean a2=true;

for(int i=0;i<a1.length();i++){

if(a[i]<48||a[i]>57){

a2=false;break;

}

}

try{

if(a2==false){

throw new MyException("输入错误,请重新输入");

}

else{

a3=false;

int a4=Integer.parseInt(a1);

if(a4<60){System.out.println("不及格");}

else if(a4>=60&&a4<70){System.out.println("及格");}

else if(a4>=70&&a4<80){System.out.println("");}

else if(a4>=80&&a4<90){System.out.println("");}

else if(a4>=90){System.out.println("");}

}

}

catch(MyException e){

System.out.println(e);

}

}

input.close();

}

 

}

截图:

 

posted @ 2016-11-25 19:43  Z鳴  阅读(128)  评论(0编辑  收藏  举报