1、实验3实验报告异常处理学习资料信息工程学院Java语言课内实习报告 (201201学年第 二学期) 实习题目:异常处理 姓 名: 学 号: 专 业:计算机科学与技术 年级班级:一、实习目的 掌握Java的异常处理机制及相关实现方法能够在程序设计中熟练运用异常及相关类及对象。二、实习设计过程实验题1 在程序中产生一个ArithmeticException 类型被0除的异常,并用catch 语句捕获这个异常。最后通过ArithmeticException 类的对象e 的方法getMessage 给出异常的具体类型并显示出来。设计如下:try int c=a/b; System.out.print(
2、c); catch(ArithmeticException e) System.out.println(Caught ArithmeticException); System.out.println(e.getMessage(): + e.getMessage(); 运行结果如下: 实验题2 在一个类的静态方法methodOne()方法内使用throw 产生异常,使用throws子句抛出methodOne()的异常,在main方法中捕获处理异常。设计如下:static void methodOne() throws ArrayIndexOutOfBoundsException throw ne
3、w ArrayIndexOutOfBoundsException(数组下标越界); try methodOne(); catch(ArrayIndexOutOfBoundsException e ) System.out.println(错误是:+e); 运行结果如下:实验题3 编写一个程序,输入一个班某门课程成绩,统计及格人数、不及格人数平均分。设计一个异常类,当输入的成绩小0分或大于100分时,抛出异常,程序将捕捉这个异常,并作出相应处理。设计如下:/异常类部分class InterException extends Exception String message; public Int
4、erException(double m) message= 输入的成绩+m+不合法+请检查您的输入是否有误!; public String toString() return message; /成绩处理部分public void setRecord(double record) throws InterException if(record100|record=60) n1+; else n2+; sum+=record; /Main函数中try -catch块部分:try double sore=reader.nextDouble(); /System.out.println(sore)
5、; record.setRecord(sore); catch(InterException e) System.out.println(e.toString(); -i; 运行结果如下:实验题4 创建异常类的练习。需要使用3个Java程序来实现:创建银行类Bank:/存钱部分void deposite(double dAmount) if(dAmount0) this.dAmount=dAmount; this.balance+=dAmount; /取钱部分 void withdrawal(double dAmount) throws InsufficientFundsException /
6、System.out.println(取款金额为:+dAmount); if(dAmountthis.balance) throw new InsufficientFundsException(this.balance,dAmount); else this.balance-=dAmount; /显示余额部分 double show_balance() return this.balance;创建异常类:/异常类的构造public InsufficientFundsException(double balance,double dAmount) this.balance=balance; th
7、is.dAmount=dAmount; /异常信息 String excepMesagge() Bank bank=new Bank(this.balance); return 您的取款金额为+this.dAmount+,但是账户余额仅为+bank.show_balance()+,操作不合法!; 创建主类: ExceptionDemo: Bank bank=new Bank(2000); bank.deposite(100); try bank.withdrawal(4000); catch(InsufficientFundsException e) System.out.println(e.
8、toString():+e.toString(); System.out.println(e.excepMesagge(); finally System.out.println(操作退出!); 运行结果如下:3、调试过程中存在问题分析实验题目4中异常类创建过程中,用到Bank类中的方法,初始化过程中,Bank类中的信息与异常类的连接存在问题,导致信息显示与想要的结果不匹配。解决办法:String excepMesagge() Bank bank=new Bank(this.balance); return 您的取款金额为+this.dAmount+,但是账户余额仅+bank.show_bal
9、ance()+,操作不合法!; 在这个方法中创建Bank类的对象,构造用异常类中的this.balance。四、心得、体会与建议在这次实验过程中,异常类的构造我自己感觉还是不太熟悉。方法用throws抛出异常,方法内部用throw关键字产生异常基本掌握。这部分自己还需要加大练习力度。附录:题目1代码:public class TryArithmeticException /* * param args */ static int a=1; static int b=0; public static void main(String args) / TODO Auto-generated met
10、hod stub try int c=a/b; System.out.print(c); catch(ArithmeticException e) System.out.println(Caught ArithmeticException); System.out.println(e.getMessage(): + e.getMessage(); 题目2代码:public class TryThrow /* * param args */ static int a=new int6; static void methodOne() throws ArrayIndexOutOfBoundsExc
11、eption throw new ArrayIndexOutOfBoundsException(数组下标越界); public static void main(String args) / TODO Auto-generated method stub try methodOne(); catch(ArrayIndexOutOfBoundsException e ) System.out.println(错误是:+e); 题目3代码:import java.util.Scanner;SuppressWarnings(serial)class InterException extends Ex
12、ception String message; public InterException(double m) message= 输入的成绩+m+不合法+请检查您的输入是否有误!; public String toString() return message; public class TryRecord int n1,n2; double sum=0; public void setRecord(double record) throws InterException if(record100|record=60) n1+; else n2+; sum+=record; public st
13、atic void main(String args) TryRecord record=new TryRecord(); SuppressWarnings(resource) Scanner reader=new Scanner(System.in); System.out.print(请输入班级总人数:); int n=reader.nextInt();/班级总人数 System.out.println(请您逐次输入学生课程成绩:); for(int i=0;i0) this.dAmount=dAmount; this.balance+=dAmount; void withdrawal(d
14、ouble dAmount) throws InsufficientFundsException /System.out.println(取款金额为:+dAmount); if(dAmountthis.balance) throw new InsufficientFundsException(this.balance,dAmount); else this.balance-=dAmount; double show_balance() return this.balance; public static void main(String args) / TODO Auto-generated
15、method stub InsufficientFundsException.java:SuppressWarnings(serial)public class InsufficientFundsException extends Exception /* * param args */ double balance; double dAmount; public InsufficientFundsException(double balance,double dAmount) this.balance=balance; this.dAmount=dAmount; String excepMe
16、sagge() Bank bank=new Bank(this.balance); return 您的取款金额为+this.dAmount+,但是账户余额仅为+bank.show_balance()+,操作不合法!; public static void main(String args) / TODO Auto-generated method stub ExceptionDemo.java:public class ExceptionDemo /* * param args */ public static void main(String args) / TODO Auto-generated method stub Bank bank=new Bank(2000); bank.deposite(100); try bank.withdrawal(4000); catch(InsufficientFundsException e) System.out.println(e.toString():+e.toString(); System.out.println(e.excepMesagge(); finally System.out.println(操作退出!);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1