java实验3(10)异常 - 必做

2020-05-13 10:05

班级: 13网络1班 姓名: 学号: 成绩

异常

实验目的: 1. 理解异常现象及异常的抛出机制 2. 掌握异常抛出时的处理机制 3. 掌握自定义异常的方法 4. 能够掌握在方法代码中添加抛出异常的处理 实验要求和过程 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”); } } } 分析上述程序运行后的输出的结果和原因。 3 因为除数i=0所以出错下面就没执行,跳到catch(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(\初始余额小于0异常\); } } //查询余额方法,返回当前余额 public double getBalance(){ return this.balance; } //存款方法,存款数额amount; 如果存款数目小于0抛出异常 public void deposit(double amount) throws NegativeAmountException{ if(amount>=0){ balance+=amount; }else { throw new NegativeAmountException(\存款金额小于0异常!\); } } //取款方法,取款数额amount; 如果取款数目小于0抛出异常 public void withdraw(double amount) throws NegativeAmountException{ if(amount<0){ throw new NegativeAmountException(\取款金额小于0异常!\); }else if(balance

// // // // // // // } } // Account a1 = new Account(); a1.deposit(100); a1.withdraw(-100); a1.withdraw(30); System.out.println(\ Account a2 = new Account(-10); // a2 = new Account(10); a2.deposit(-20); a2.withdraw(-10); a2.withdraw(30); a2.withdraw(3); System.out.println(\} catch (NegativeAmountException e) { e.printStackTrace(); } 3. 模仿上题中NegativeAmountException自定义异常的写法,根据下面要求写程序。 1) 自定义异常OnlyOneException与NoOprandException,并补充各自类的构造函数,参数用于保存异常发生时候的信息; 2) 添加main方法,如果没有输入命令行参数 ,抛出NoOprandException异常并退出程序的执行;如果命令行参数的数目只要一个,抛出OnlyOneException异常并退出程序的执行;否则从命令行读入两个数据,计算这两个数据的和并输出。 class OnlyOneException extends Exception { } class NoOprandException extends Exception { } class Sum { int a; int b; NoOprandException(String s) { } super(s); OnlyOneException(String s) { } super(s);

} public Sum() throws NoOprandException { a = b = 0; throw new NoOprandException(\异常!\); } public Sum(int a) throws OnlyOneException { throw new OnlyOneException(\异常!\); } public Sum(int a, int b) { } System.out.println(a + b); public class TestNegativeAmountException { public static void main(String args[]) { } } try{ } catch (NoOprandException e1) { } try{ } catch (OnlyOneException e2) { } Sum A3=new Sum(1,2); System.out.println(e1.getMessage); Sum A2=new Sum(1); System.out.println(e1.getMessage); Sum A1=new Sum(); 4. 声明一个异常类BlockedException表示账户被冻结异常。并继续第2个实验,为Account类添加一个String类型的属性satstus(表示账户是否被冻结),同时添加一个setStatus方法用于修改satstus属性。对Account类进行合理的修改(注意:当satstus的值为“blocked”时,取款、存款、查询余额等方法会发生BlockedException。并编写测试类进行相应的验证。 public class TestNegativeAmountException { public static void main(String[] args){ Account A=new Account(); try{

A.setStatus(\); A.deposit(100); A.withdraw(200); }catch( BlockedException e){ System.out.println(e.getMessage()); }catch(NegativeAmountException e){ System.out.println(e.getMessage()); } } } class BlockedException extends Exception { } class NegativeAmountException extends Exception{ class Account{ String satstus; double balance; //构造函数,余额为0; public Account(){ balance = 0; } //构造函数,余额为n,如果初始余额小于0抛出异常 public void setStatus(String s) { } public Account( double n) throws NegativeAmountException{ if(n>0){ this.balance = n; }else { throw new NegativeAmountException(\初始余额小于0异常\); } } //查询余额方法,返回当前余额 satstus=s; //NegativeAmountException异常:用消息s创建异常 NegativeAmountException(String s){ super(s); } } BlockedException(String s) { } super(s);

public double getBalance() throws BlockedException{ if(satstus!=\) else throw new BlockedException(\账户被冻结异常\); return this.balance; } //存款方法,存款数额amount; 如果存款数目小于0抛出异常 public void deposit(double amount) throws NegativeAmountException, if(satstus!=\) { BlockedException{ if(amount>=0){ balance+=amount; }else { throw new NegativeAmountException(\存款金额小于0异常!\); } } else throw new BlockedException(\账户被冻结异常\); } //取款方法,取款数额amount; 如果取款数目小于0抛出异常 public void withdraw(double amount) throws NegativeAmountException, if(satstus!=\) { BlockedException{ if(amount<0){ throw new NegativeAmountException(\取款金额小于0异常!\); }else if(balance


java实验3(10)异常 - 必做.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:企业方前期需提供的部份资料清单

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: