实验9自定义异常的使用.doc

上传人:marr****208 文档编号:141781983 上传时间:2020-08-12 格式:DOC 页数:5 大小:40.50KB
返回 下载 相关 举报
实验9自定义异常的使用.doc_第1页
第1页 / 共5页
实验9自定义异常的使用.doc_第2页
第2页 / 共5页
实验9自定义异常的使用.doc_第3页
第3页 / 共5页
实验9自定义异常的使用.doc_第4页
第4页 / 共5页
实验9自定义异常的使用.doc_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《实验9自定义异常的使用.doc》由会员分享,可在线阅读,更多相关《实验9自定义异常的使用.doc(5页珍藏版)》请在金锄头文库上搜索。

1、1)在定义一个银行类 Bank 时, 有 deposit、withdraw、showBalance 等方法。若取钱数大于余额则作为异常处理(InsufficientFundsException)。产生异常的条件是余额少于取额,因此是否抛出异常要判断条件,要定义好自己的异常类。package ex91;public class Bank public double total=0; Bank(double d) total=d; public void deposit(double d) total+=d; public void withdraw(double d) total-=d; pub

2、lic void showBalance() System.out.println(total); package ex91;public class BankException extends Exception String message; BankException(double d) message=d+非法取款余额; public String getMessage() return message; package ex91;import java.util.Scanner;public class BankTest /* * param args */ public stati

3、c void main(String args) throws BankException / TODO Auto-generated method stubBank bk=new Bank(1236.123);bk.showBalance();Scanner sc = new Scanner(System.in); int i=sc.nextInt();bk.withdraw(i); if(bk.total0) BankException be=new BankException(i);throw(be); else bk.showBalance(); (2)定义 Triangle 类用于表

4、示三角形,其任意两个边的和必须大于第三条边。定义 IllegalTriangleException 用于声明任何违反以上规则的输入。Triangle类的构造方法如下:public Triangle(double side1, double side2, double side3) throwsIllegalTriangleException / Implement itpackage ex92;import java.io.IOException;class IllegalTriangleException extends Exception / private static final lo

5、ng serialVersionUID = 1L; package ex92;import java.io.IOException;public class Test public static void main(String args) throws IOException Triangle t1; try t1 = new Triangle(3, 1, 2); catch (IllegalTriangleException e) e.printStackTrace(); System.out.println(不合法的三角形); return; System.out.println(sid

6、e1= + t1.side1 + ,side2= + t1.side2 + ,side3= + t1.side3); package ex92;class Triangle double side1; double side2; double side3; public Triangle(double side1, double side2, double side3) throws IllegalTriangleException super(); if (side1 + side2 = side3 | side1 + side3 = side2 | side2 + side3 = side

7、1) throw new IllegalTriangleException(); this.side1 = side1; this.side2 = side2; this.side3 = side3; (3)要求声明定义 2 个异常类:NoLetterException 类和 NoDigitException类。再定义一个 People 类,该类中的 void printLetter(char c) throws 方法抛出NoLetterException 异常,void printDigit(char c)方法抛出 NoDigitException 异常。主类 ExceptionExampl

8、e 对 2 个方法分别测试。package ex93;public class ExceptionExample public static void main (String args ) People people=new People( ); for(int i=0;i128;i+) try people.printLetter(char)i); /调用people对象的printLetter方法并处理异常 catch(NoLetterException e) System.out.println(发生异常:+e.getMessage(); e.print(); for(int i=0;

9、i=a&c=0&c=9) throw new NoDigitException(); else System.out.println(请输出该字符:+c); (4)CircleArea 类是一个命令行输入的计算器,用于计算圆的面积,程序从命令行接收半径。如果命令行输入的是非数字,则程序抛出异常,显示消息通知用户必须输入数字。e 类用于表示三角形,其任意两个边的和必须大于第三条边。定义 IllegalTriangleException 用于声明任何违反以上规则的输入。Triangle类的构造方法如下:public Triangle(double side1, double side2, doub

10、le side3) throwsIllegalTriangleException / Implement itpackage ex94;import java.io.BufferedReader;import java.io.InputStreamReader;public class TestCircle public static void main(String args) while(true) System.out.println(Please enter a radius of the circle: );tryBufferedReader br = new BufferedRea

11、der(new InputStreamReader(System.in);double radius = Double.valueOf(br.readLine().trim();System.out.println(Your enter the radius is: + radius);double area = Math.PI * radius * radius;System.out.println(The circle area is: + area);break; catch(Exception e) System.out.println(Your input is not the number,Please enter the correct radius!);

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 其它相关文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号