实验3实验报告异常处理学习资料.docx

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

实验3实验报告异常处理学习资料.docx

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

实验3实验报告异常处理学习资料.docx

实验3实验报告异常处理学习资料

信息工程学院

Java语言课内实习报告

(201~201学年第二学期)

 

实习题目:

异常处理

 

姓名:

学号:

专业:

计算机科学与技术

年级班级:

 

一、实习目的

掌握Java的异常处理机制及相关实现方法能够在程序设计中熟练运用异常及相关类及对象。

二、实习设计过程

实验题1在程序中产生一个ArithmeticException类型被0除的异常,并用catch语句捕获这个异常。

最后通过ArithmeticException类的对象e的方法getMessage给出异常的具体类型并显示出来。

设计如下:

try{intc=a/b;

System.out.print(c);

}catch(ArithmeticExceptione){

System.out.println("CaughtArithmeticException");

System.out.println("e.getMessage():

"+e.getMessage());

}

运行结果如下:

实验题2在一个类的静态方法methodOne()方法内使用throw产生异常,使用throws子句抛出methodOne()的异常,在main方法中捕获处理异常。

设计如下:

staticvoidmethodOne()throwsArrayIndexOutOfBoundsException

{

thrownewArrayIndexOutOfBoundsException("数组下标越界");

}

try{methodOne();

}

catch(ArrayIndexOutOfBoundsExceptione){

System.out.println("错误是:

"+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);

if(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{

System.out.println("操作退出!

");

}

运行结果如下:

3、调试过程中存在问题分析

实验题目4中异常类创建过程中,用到Bank类中的方法,初始化过程中,Bank类中的信息与异常类的连接存在问题,导致信息显示与想要的结果不匹配。

解决办法:

StringexcepMesagge(){

Bankbank=newBank(this.balance);

return"您的取款金额为"+this.dAmount+",但是账户余额仅"+bank.show_balance()+",操作不合法!

";}

在这个方法中创建Bank类的对象,构造用异常类中的this.balance。

四、心得、体会与建议

在这次实验过程中,异常类的构造我自己感觉还是不太熟悉。

方法用throws抛出异常,方法内部用throw关键字产生异常基本掌握。

这部分自己还需要加大练习力度。

附录:

题目1代码:

publicclassTryArithmeticException{

/**

*@paramargs

*/

staticinta=1;

staticintb=0;

publicstaticvoidmain(String[]args){

//TODOAuto-generatedmethodstub

try{

intc=a/b;

System.out.print(c);

}catch(ArithmeticExceptione){

System.out.println("CaughtArithmeticException");

System.out.println("e.getMessage():

"+e.getMessage());

}

}

}

题目2代码:

publicclassTryThrow{

/**

*@paramargs

*/

staticint[]a=newint[6];

staticvoidmethodOne()throwsArrayIndexOutOfBoundsException

{

thrownewArrayIndexOutOfBoundsException("数组下标越界");

}

publicstaticvoidmain(String[]args){

//TODOAuto-generatedmethodstub

try{

methodOne();

}

catch(ArrayIndexOutOfBoundsExceptione){

System.out.println("错误是:

"+e);

}

 

}

}

 

题目3代码:

importjava.util.Scanner;

@SuppressWarnings("serial")

classInterExceptionextendsException{

Stringmessage;

publicInterException(doublem){

message="输入的成绩"+m+"不合法"+"请检查您的输入是否有误!

";

}

publicStringtoString(){

returnmessage;

}

}

publicclassTryRecord{

intn1,n2;

doublesum=0;

publicvoidsetRecord(doublerecord)throwsInterException{

if(record>100||record<0){

thrownewInterException(record);

}

else{

if(record>=60)

{

n1++;

}

else{

n2++;

}

sum+=record;

}

 

}

publicstaticvoidmain(String[]args){

TryRecordrecord=newTryRecord();

@SuppressWarnings("resource")

Scannerreader=newScanner(System.in);

System.out.print("请输入班级总人数:

");

intn=reader.nextInt();//班级总人数

System.out.println("请您逐次输入学生课程成绩:

");

for(inti=0;i

System.out.print("第"+(i+1)+"个同学的成绩为:

");

try{

doublesore=reader.nextDouble();

//System.out.println(sore);

record.setRecord(sore);

}

catch(InterExceptione){

System.out.println(e.toString());

--i;

}

}

System.out.println("及格人数为:

"+record.n1);

System.out.println("不及格人数为:

"+record.n2);

doubleavg=record.sum/n;

System.out.println("平均分为:

"+avg);

}

}

题目4代码:

Bank.java:

publicclassBank{

/**

*@paramargs

*/

doublebalance;

doubledAmount;

publicBank(doublebalance){

this.balance=balance;

}

publicBank(){

//TODOAuto-generatedconstructorstub

}

voiddeposite(doubledAmount){

if(dAmount>0)

{this.dAmount=dAmount;

this.balance+=dAmount;}

}

voidwithdrawal(doubledAmount)throwsInsufficientFundsException{

//System.out.println("取款金额为:

"+dAmount);

if(dAmount>this.balance){

thrownewInsufficientFundsException(this.balance,dAmount);

}

else{

this.balance-=dAmount;}

}

doubleshow_balance(){

returnthis.balance;

}

 

publicstaticvoidmain(String[]args){

//TODOAuto-generatedmethodstub

}

}

InsufficientFundsException.java:

@SuppressWarnings("serial")

publicclassInsufficientFundsExceptionextendsException{

/**

*@paramargs

*/

doublebalance;

doubledAmount;

publicInsufficientFundsException(doublebalance,doubledAmount){

this.balance=balance;

this.dAmount=dAmount;

}

StringexcepMesagge(){

Bankbank=newBank(this.balance);

return"您的取款金额为"+this.dAmount+",但是账户余额仅为"+bank.show_balance()+",操作不合法!

";

}

publicstaticvoidmain(String[]args){

//TODOAuto-generatedmethodstub

}

}

ExceptionDemo.java:

publicclassExceptionDemo{

/**

*@paramargs

*/

publicstaticvoidmain(String[]args){

//TODOAuto-generatedmethodstub

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{

System.out.println("操作退出!

");

}

}

}

 

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

当前位置:首页 > PPT模板

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

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