实验3实验报告异常处理学习资料Word文件下载.docx

上传人:b****4 文档编号:16843497 上传时间:2022-11-26 格式:DOCX 页数:12 大小:159.24KB
下载 相关 举报
实验3实验报告异常处理学习资料Word文件下载.docx_第1页
第1页 / 共12页
实验3实验报告异常处理学习资料Word文件下载.docx_第2页
第2页 / 共12页
实验3实验报告异常处理学习资料Word文件下载.docx_第3页
第3页 / 共12页
实验3实验报告异常处理学习资料Word文件下载.docx_第4页
第4页 / 共12页
实验3实验报告异常处理学习资料Word文件下载.docx_第5页
第5页 / 共12页
点击查看更多>>
下载资源
资源描述

实验3实验报告异常处理学习资料Word文件下载.docx

《实验3实验报告异常处理学习资料Word文件下载.docx》由会员分享,可在线阅读,更多相关《实验3实验报告异常处理学习资料Word文件下载.docx(12页珍藏版)》请在冰豆网上搜索。

实验3实验报告异常处理学习资料Word文件下载.docx

}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{

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 初中教育 > 中考

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1