【异常处理习题】(附答案)

1.public class TestApp{
public static void main(String[] args){
try{
int i = 0;
int j = 1 / i;
String myname=null;
if(myname.length()>2)
System.out.print(“1”);
}catch(NullPointerException e){
System.out.print(“2”);
}catch(Exception e){
System.out.print(“3”);
}
}
}
分析上述程序运行后的输出的结果和原因。
原因:在try中,出现错误的语句是int j = 1 / i;后面语句不执行,所以不打印1,该错误属于“ArithmeticException”类型的错误,所以空指针错误无法捕获到,不打印2。而catch(Exception e){System.out.print(“3”);Exception是错误类型的父类,所以能够处理该错误,因此打印3。
2.下面是一个名称为NegativeAmountException的自定义异常类,表示一个不正常的银行账目事件类。填充下面的语句,完成该类的编写。

class NegativeAmountException extends Exception{
//NegativeAmountException异常:用消息s创建异常
NegativeAmountException(String s){
super(s);
}
}





class Account{
double balance;
//构造函数,余额为0;
public Account(){
balance = 0;
}
//构造函数,余额为n,如果初始余额小于0抛出异常
public Account( double n) throws NegativeAmountException{
if(n>0){
this.balance = n;
}else {
	throw new NegativeAmountException("该余额异常") ;
}
}
//查询余额方法,返回当前余额
public double getBalance(){
return this.balance;
}
//存款方法,存款数额amount; 如果存款数目小于0抛出异常
public void deposit(double amount)throws NegativeAmountException{
if(amount>=0){
balance+=amount;
}else {
throw new NegativeAmountException("存款出错");
}
}
//取款方法,取款数额amount; 如果取款数目小于0抛出异常
public  void withdraw(double amount)throws NegativeAmountException{
if(amount<0){
throw new NegativeAmountException("操作错误");
}else if(balance<amount){
throw new NegativeAmountException("取款出错");
}else{
balance-=amount;  
}
}
}

3.模仿上题中NegativeAmountException自定义异常的写法,根据下面要求写程序。

  1. 自定义异常OnlyOneException与NoOprandException,并补充各自类的构造函数,参数用于保存异常发生时候的信息;
  2. 创建一个测试主类TestMyException,添加main方法,从命令行参数读入两个数,计算这两个数的和并输出。如果参数的数目只要一个,抛出OnlyOneException异常并退出程序的执行;如果没有参数 ,抛出NoOprandException异常并退出程序的执行;
public class OnlyOneException extends Exception{
	
	OnlyOneException(String s){
	super(s);
	}
	}

public class NoOprandException extends Exception{
	
	NoOprandException(String s){
	super(s);
	}
	}

import java.util.Scanner;

public class TestMyException {

	public static void main(String[] args)throws OnlyOneException,NoOprandException {
		// TODO Auto-generated method stub
		Scanner numb  = new Scanner(System.in);
		System.out.println("请输入第一个数字");

		String a = numb.nextLine();
		if(a.length()==0)
		{
		throw new NoOprandException("没有参数");
		}
		
		System.out.println("请输入第二个数字");
		String b = numb.nextLine();
		if(b.length()==0)
		{
		throw new OnlyOneException("只有一个参数");
		}
		int c = Integer.valueOf(a);
		int d = Integer.valueOf(b);
		int sum = c+d;
		System.out.println("相加后的数值为:"+sum);
		numb.close();


	}



}

4.为下列实验7(5),
abstract class Account {
private String name,aid;
private double cash;
public void setCash(double cash) {
this.cash = cash;
}
public String getName() {
return name;
}
public double getCash() {
return cash;
}
public Account() {
//super(“”);
}
public Account(String a,String b,double c){
//super(“”);
this.name=a;
this.aid=b;
this.cash=c;
}
abstract public void deposit(double a);
abstract public void withdraw(double a)
abstract public double getBalance();
}
class CashAccount extends Account{
String s;
public CashAccount(String a,String b,double c,String d){
super(a,b,c);
this.s=d;
}
public void deposit(double a){
setCash(getCash()+a);
}
public void withdraw(double a) {
if(a>getCash())
return;
else
setCash(getCash()-a);
}
public double getBalance(){
return getCash();
}
public String change(String a){
s=a;
return s;
}
}
class CreditAccount extends Account{
int year,lineofcredit;
public CreditAccount(String a,String b,double c,int d){
super(a,b,c);
this.year=d;
}
public void deposit(double a){
setCash(getCash()+a);

}
public void withdraw(double a){
     if(year>=20)lineofcredit=10000;
    if(year>=15)lineofcredit=5000;
    if(year>=10)lineofcredit=2000;
    if(year>=5)lineofcredit=1000;
    else lineofcredit=0;
    if(a>(getCash()+lineofcredit))System.out.println("用户请重新输入:");
   else setCash(getCash()-a);
    
}
public double getBalance(){
    return getCash();
}
public double  findOverDraw(){
    if(getCash()<0) return Math.abs(getCash());
    else return 0;
}

}
public class Test{
public static void main(String args[]){
CashAccount debit=new CashAccount(“Jhon”,“jhon123456”,500,“借记卡”);
debit.withdraw(300);
System.out.println(“剩余钱:”+debit.getBalance());
debit.withdraw(300);
System.out.println(“剩余钱:”+debit.getBalance());
}
}

添加一个取款异常WithdrawException,请定义异常类WithdrawException,要求:
1)异常对象记录取款发生时的帐户余额、取款额还有取款人。
2)覆盖继承于超类的方法getMessage(),按以下格式返回信息:
取款人,账号余额,取款额,原因:透支。
3)修改CashAccount的取款方法,当用户取款超出额度时,抛出异常。
4)编写一个可执行类Test,创建一个CashAccount的对象,其初始余额为500,连续取款2次,每次300,写出运行结果。

package hstu;

abstract class Account {
    private String name,aid;
    private double cash;

    
    public void setCash(double cash) {
        this.cash = cash;
    }
    public String getName() {
        return name;
    }
    public double getCash() {
        return cash;
    }
    public Account() {
        //super("");
    }
    public Account(String a,String b,double c){
        //super("");
        this.name=a;
        this.aid=b;
        this.cash=c;
    }
    abstract public void deposit(double a);
    abstract public void withdraw(double a);
    abstract public double getBalance();
}
package hstu;

class CashAccount extends Account {
    String s;

    public CashAccount(String a, String b, double c, String d) {
        super(a, b, c);
        this.s = d;
    }

    public void deposit(double a) {
        setCash(getCash() + a);
    }

    public void withdraw(double a) {

        try{
            if (a<getCash()){
                setCash(getCash()-a);
            }

            else {
                throw new WithdrawException("余额不足或输入错误");
            }

        }catch (WithdrawException e){
            System.out.println(e + ",目前账户余额为:" +
                    this.getBalance() +
                    "而取款额为:" + a + "取款人为:" + getName());
        }
        /*try {
            if (a < getCash()&&this.getState()=="on") {
                setCash(getCash() - a);
            }
            else if (a > getCash()) {
                throw new WithdrawException("余额不足或输入错误");
            }
            if (this.getState() == "blocked") {
                throw new BlockedException("当前账户已冻结");
            }
        } catch (WithdrawException e) {
            System.out.println(e + ",目前账户余额为:" +
                    this.getBalance() +
                    "而取款额为:" + a + "取款人为:" + getName());
            this.getMssage(a);
        } catch (BlockedException e) {
            System.out.println(e + "当前状态为:" + getState());
        }*/

    }

    public double getBalance() {
        return getCash();
    }

    public String change(String a) {
        s = a;
        return s;
    }

    public void getMssage(double a) {
        System.out.println("取款人" + getName() + "\n" +
                "账户余额" + getBalance() + "\n" +
                "取款额" + a + "\n" + "原因:透支");
    }
}

package hstu;




public class Test{
    public static void main(String args[]){
        CashAccount debit=new CashAccount("Jhon","jhon123456",500,"借记卡");
//        debit.setState("blocked");
        debit.withdraw(300);
//        debit.setState("on");
//        debit.withdraw(300);
        System.out.println("剩余钱:"+debit.getBalance());
        debit.withdraw(300);
        System.out.println("剩余钱:"+debit.getBalance());
    }
}


5.继续上一个小题,如果为Account类添加一个String类型的属性 ststus,当它的值为“blocked”时,取款时发生BlockedException,重写上一个小题(注,取款方法要抛出上述两个异常),写出异常类BlockedException、新的取款方法,并编写程序验证。

package hstu;

abstract class Account {
    private String name,aid;
    private double cash;

    public String getState() {
        return State;
    }

    public void setState(String state) {
        State = state;
    }

    private String State="on";
    public void setCash(double cash) {
        this.cash = cash;
    }
    public String getName() {
        return name;
    }
    public double getCash() {
        return cash;
    }
    public Account() {
        //super("");
    }
    public Account(String a,String b,double c){
        //super("");
        this.name=a;
        this.aid=b;
        this.cash=c;
    }
    abstract public void deposit(double a);
    abstract public void withdraw(double a);
    abstract public double getBalance();
}
package hstu;

class CashAccount extends Account {
    String s;

    public CashAccount(String a, String b, double c, String d) {
        super(a, b, c);
        this.s = d;
    }

    public void deposit(double a) {
        setCash(getCash() + a);
    }

    public void withdraw(double a) {
        try {
            if (a < getCash()&&this.getState()=="on") {
                setCash(getCash() - a);
            }
            else if (a > getCash()) {
                throw new WithdrawException("余额不足或输入错误");
            }
            if (this.getState() == "blocked") {
                throw new BlockedException("当前账户已冻结");
            }
        } catch (WithdrawException e) {
            System.out.println(e + ",目前账户余额为:" +
                    this.getBalance() +
                    "而取款额为:" + a + "取款人为:" + getName());
            this.getMssage(a);
        } catch (BlockedException e) {
            System.out.println(e + "当前状态为:" + getState());
        }

    }

    public double getBalance() {
        return getCash();
    }

    public String change(String a) {
        s = a;
        return s;
    }

    public void getMssage(double a) {
        System.out.println("取款人" + getName() + "\n" +
                "账户余额" + getBalance() + "\n" +
                "取款额" + a + "\n" + "原因:透支");
    }
}

package hstu;




public class Test{
    public static void main(String args[]){
        CashAccount debit=new CashAccount("Jhon","jhon123456",500,"借记卡");
        debit.setState("blocked");
        debit.withdraw(300);
        debit.setState("on");
        debit.withdraw(300);
        System.out.println("剩余钱:"+debit.getBalance());
        debit.withdraw(300);
        System.out.println("剩余钱:"+debit.getBalance());
    }
}


public class BlockedException extends Exception{
    BlockedException(String s) {
        super(s);
    }
}

最后一题可以看看逻辑

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值