菜鸟学Java第四天 关于Exception,throws,throw,try ,catch

throws 声明异常
throw 抛出异常
try 捕捉异常
catch 报出异常执行的操作
finally  必须执行的代码 如:关闭Connection 

软件的健壮性反映了程序代码对各种异常操作妥善处理能力的大小。那什么是异常呢?异常(Exception)是程序在执行过程中临时发生的“意外事故”,导致程序不能正常地运行的事件。

异常与错误之间的区别

(1)语法错误是程序代码的语法完整性缺陷,如语句的末尾忘记加分好等。包含语法错误的程序是不能执行的。

(2)逻辑错误是程序运行结果中发省的错误,如一元二次方程实数跟的程序,可能因为逻辑错误而导致结果为非实数等。包含逻辑错误的程序是可以正常运行的,但结果是错误的。

(3)异常与错误不同,异常是指在程序运行过程中用户的某种操作或误操作导致程序无法继续运行下去。异常会导致软件在运行过程中“突然崩溃”,例如操作系统软件的异常就是“死机”、“突然关机”、“自动重启”等现象的根源。

下面通过求=的过程来展示怎么样自定义属于自己的Exception,此程序说明如下:从键盘上任意输入两个正整数m和n的值,计算其排列组合的值。其中对于程序运行后输入的错误数据,要求作出以下几种异常处理:

(1)       输入的字符串必须能够分解出整数。如果输入的是小数或者字符则提示“应输入整数数据”;

(2)       如果输入的数是负数,则提示“请输入正整数”;

(3)       如果m<n则提示“m>n”;

NotPositiveNum.java

package guan;

 

import javax.swing.JOptionPane;

 

public class NotPositiveNum extends Exception {

 

   

    private static final long serialVersionUID = 1L;

    private int x;

    public NotPositiveNum(int x) {

       this.x = x;

    }

   

    public void printStackTrace() {

         JOptionPane.showMessageDialog(null, "请输入正整数");

    }

}

NotBigthan.java

package guan;

 

import javax.swing.JOptionPane;

 

public class NotBigthan extends Exception {

    private static final long serialVersionUID = 1L;

    private int m;

    private int n;

 

    public NotBigthan(int m, int n) {

       this.m = m;

       this.n = n;

    }

    public void printStackTrace() {

       // TODO Auto-generated method stub

       JOptionPane.showMessageDialog(null,

              "m>n");

    }

 

}

Cmn.java

package guan;

 

public class Cmn {

    private int m;

    private int n;

    public Cmn(int m, int n) {

       this.m = m;

       this.n = n;

    }

    public static int getN(int x)

    {

       int result=x;

       while(x>1)

       {

           result*=x-1;

           x--;

       }

       return result;

    }

    public static int getResult(int m,int n)

    {

       return getN(m)/(getN(n)*getN(m-n));

    }

    public static void vali(int x)throws NotPositiveNum

    {

       if(x<0)throw (new NotPositiveNum(x));

    }

    public static void validation(int m,int n)throws NotBigthan,NotPositiveNum

    {

      

       if(m<n)throw(new NotBigthan(m, n));

    }

   

 

}

Client.java

package guan;

 

import javax.swing.JOptionPane;

 

public class Client {

 

    /**

     * @param args

     */

    public static void main(String[] args) {

 

       int m = 5, n = 3, r = 0;

       String str1, str2;

       str1 = JOptionPane.showInputDialog("m=");

 

       try {

           m = Integer.parseInt(str1);

           Cmn.vali(m);

           str2 = JOptionPane.showInputDialog("n=");

           n = Integer.parseInt(str2);

           Cmn.vali(n);

           Cmn.validation(m, n);

       } catch (NotPositiveNum e) {

           e.printStackTrace();

       } catch (NotBigthan e) {

           e.printStackTrace();

       } catch (NumberFormatException e) {

           JOptionPane.showMessageDialog(null,

                  "应输入整数数据");

       }

       if (r != 0) {

           r = Cmn.getResult(m, n);

           JOptionPane.showMessageDialog(null, r);

       }

    }

}

 

posted @ 2011-11-01 19:55  IT骆驼翔子  阅读(359)  评论(0编辑  收藏  举报