实验3实验报告异常处理学习资料Word文件下载.docx
《实验3实验报告异常处理学习资料Word文件下载.docx》由会员分享,可在线阅读,更多相关《实验3实验报告异常处理学习资料Word文件下载.docx(12页珍藏版)》请在冰豆网上搜索。
}catch(ArithmeticExceptione){
System.out.println("
CaughtArithmeticException"
);
e.getMessage():
"
+e.getMessage());
}
运行结果如下:
实验题2在一个类的静态方法methodOne()方法内使用throw产生异常,使用throws子句抛出methodOne()的异常,在main方法中捕获处理异常。
staticvoidmethodOne()throwsArrayIndexOutOfBoundsException
{
thrownewArrayIndexOutOfBoundsException("
数组下标越界"
}
try{methodOne();
}
catch(ArrayIndexOutOfBoundsExceptione){
错误是:
"
+e);
实验题3编写一个程序,输入一个班某门课程成绩,统计及格人数、不及格人数平均分。
设计一个异常类,当输入的成绩小0分或大于100分时,抛出异常,程序将捕捉这个异常,并作出相应处理。
//异常类部分
classInterExceptionextendsException{
Stringmessage;
publicInterException(doublem){
message="
输入的成绩"
+m+"
不合法"
+"
请检查您的输入是否有误!
;
publicStringtoString(){
returnmessage;
}
//成绩处理部分
publicvoidsetRecord(doublerecord)throwsInterException{
if(record>
100||record<
0){
thrownewInterException(record);
else{
if(record>
=60)
{
n1++;
}
else{
n2++;
sum+=record;
//Main函数中try-catch块部分:
try{doublesore=reader.nextDouble();
//System.out.println(sore);
record.setRecord(sore);
}catch(InterExceptione){
System.out.println(e.toString());
--i;
实验题4创建异常类的练习。
需要使用3个Java程序来实现:
创建银行类Bank:
//存钱部分
voiddeposite(doubledAmount){
if(dAmount>
0)
{this.dAmount=dAmount;
this.balance+=dAmount;
}
//取钱部分
voidwithdrawal(doubledAmount)throwsInsufficientFundsException{
//System.out.println("
取款金额为:
+dAmount);
this.balance){
thrownewInsufficientFundsException(this.balance,dAmount);
}else{
this.balance-=dAmount;
}
//显示余额部分
doubleshow_balance(){
returnthis.balance;
创建异常类:
//异常类的构造
publicInsufficientFundsException(doublebalance,doubledAmount){
this.balance=balance;
this.dAmount=dAmount;
//异常信息
StringexcepMesagge(){
Bankbank=newBank(this.balance);
return"
您的取款金额为"
+this.dAmount+"
但是账户余额仅为"
+bank.show_balance()+"
操作不合法!
创建主类:
ExceptionDemo:
Bankbank=newBank(2000);
bank.deposite(100);
try{
bank.withdrawal(4000);
catch(InsufficientFundsExceptione){
System.out.println("
e.toString():
+e.toString());
System.out.println(e.excepMesagge());
finally{
操作退出!
3、调试过程中存在问题分析
实验题目4中异常类创建过程中,用到Bank类中的方法,初始化过程中,Bank类中的信息与异常类的连接存在问题,导致信息显示与想要的结果不匹配。
解决办法:
StringexcepMesagge(){
但是账户余额仅"
在这个方法中创建Bank类的对象,构造用异常类中的this.balance。
四、心得、体会与建议
在这次实验过程中,异常类的构造我自己感觉还是不太熟悉。
方法用throws抛出异常,方法内部用throw关键字产生异常基本掌握。
这部分自己还需要加大练习力度。
附录:
题目1代码:
publicclassTryArithmeticException{
/**
*@paramargs
*/
staticinta=1;
staticintb=0;
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
try{
intc=a/b;
题目2代码:
publicclassTryThrow{
staticint[]a=newint[6];
staticvoidmethodOne()throwsArrayIndexOutOfBoundsException
try{
methodOne();
题目3代码:
importjava.util.Scanner;
@SuppressWarnings("
serial"
)
publicclassTryRecord{
intn1,n2;
doublesum=0;
publicvoidsetRecord(doublerecord)throwsInterException{
TryRecordrecord=newTryRecord();
@SuppressWarnings("
resource"
Scannerreader=newScanner(System.in);
System.out.print("
请输入班级总人数:
intn=reader.nextInt();
//班级总人数
System.out.println("
请您逐次输入学生课程成绩:
for(inti=0;
i<
n;
i++){
System.out.print("
第"
+(i+1)+"
个同学的成绩为:
);
try{
doublesore=reader.nextDouble();
catch(InterExceptione){
及格人数为:
+record.n1);
不及格人数为:
+record.n2);
doubleavg=record.sum/n;
平均分为:
+avg);
题目4代码:
Bank.java:
publicclassBank{
doublebalance;
doubledAmount;
publicBank(doublebalance){
publicBank(){
//TODOAuto-generatedconstructorstub
voiddeposite(doubledAmount){
InsufficientFundsException.java:
publicclassInsufficientFundsExceptionextendsException{
publicInsufficientFundsException(doublebalance,doubledAmount){
ExceptionDemo.java:
publicclassExceptionDemo{