自定义异常的使用

要求:

掌握异常的概念以及如何定义、抛出和捕捉处理异常。

实验课前检查预习情况,将程序代码写在作业本上。

内容1

在定义银行类Bank,若取钱数大于余额则作为异常处理(InsufficientFundsException)

思路:产生异常的条件是余额少于取额,因此是否抛出异常要判断条件。

取钱是withdrawal方法中定义的动作,因此在该方法中产生异常。

处理异常安排在调用withdrawal的时候,因此withdrawal方法要声明异常,由上级方法调用。

import java.util.*;

 

//static int x=100;

class InsufficientFundsException extends Exception {

    // double x;

    String message;

 

    InsufficientFundsException(int n) {

       message = "您的余额不足,无法取出现金";

       // System.out.println("您的余额不足,无法取出现金");

    }

 

    public String getMessage() {

       return message;

    }

}

 

class A {

    public void f(int n) throws InsufficientFundsException {

       // int x=100;

       if (n > 1000) {

           InsufficientFundsException ex = new InsufficientFundsException(n);

           throw (ex);

       } else {

           System.out.println("所取金额为:" + n);

           System.out.println("剩余金额为:" + (1000 - n));

       }

    }

}

 

public class bank {

    public static void main(String[] args) {

       // TODO Auto-generated method stub

       A a = new A();

       int m;

       Scanner reader = new Scanner(System.in);

       System.out.println("输入操作类型:");

       m = reader.nextInt();

       if (m == 0) {

           System.out.println("输入取款金额:");

           int money1 = reader.nextInt();

           // System.out.println("输入存款金额:");

           // int money2=reader.nextInt();

           try {

              a.f(money1);

           } catch (InsufficientFundsException e) {

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

           }

       } else if (m == 1) {

           System.out.println("输入存款金额:");

           int money2 = reader.nextInt();

           System.out.println("剩余金额:" + (money2 + 1000));

       }

    }

 

}

 

内容2

1.定义异常类,当从键盘输入Aa时,抛出自定义异常。

2.程序中捕获异常,查看异常抛出的位置。

3.不捕获异常,将异常往上传递。

import java.io.IOException;

import java.util.*;

 

class MyException extends Exception {

    String m;

    MyException() {

       m = "出现错误";

    }

 

    public String getM() {

       return m;

    }

}

class B {

    public void f(char n) throws MyException {

       if (n == 'a' || n == 'A') {

           MyException ex = new MyException();

           throw (ex);

       }

    }

}

public class Break {

    public static void main(String[] args) throws IOException {

       // TODO Auto-generated method stub

       char str;

       int m;

       boolean flag = true;

       B b = new B();

       Scanner reader = new Scanner(System.in);

       System.out.println("输入字符(串)");

       // str=(char)System.in.read();

       // m='str';

       while (flag) {

           try {

              str = (char) System.in.read();

              try {

                  b.f(str);

              } catch (MyException e) {

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

                  flag=false;

              }

           } catch (IOException e1) {

                 e1.printStackTrace();

           }

       }

    }

 

}

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值