文档介绍:实验报告
(2014 / 2015学年第2学期)
课程名称 JAVA程序设计
实验名称 异常程序处理、自定义异常类
实验时间 2015 年5月21日
指导单位
计算机学院/软件学院
软件工程系
指导教师
周莉
学生姓名 班级学号
学院
(系)
计算机软件学专业计算机科学与技
1
实验名称接口与接口回调 指导教师 周莉
实验类型上机I实验学时I 2 实验时间 2015-5-21
一、 实验目的
.熟悉异常的概念、异常抛出、异常的处理
. 了解Java系统的异常的类型和异常的产生
.掌握自定义异常类的处理
二、实验环境(实验设备)
.每位学生配备计算机一台
. 计,算机需安装好JDK和Jcreator
三、实验内容
.从键盘输入两个数,进行相除,显示商。当输入串中含有非数字时或除数为 。时,
过异常处理机制,使程序能正确运行。
⑴源程序:
import javax. swi ng. J Op tio nPane;
public class syl0_l{
p ublic static void main( Stri ng args[]){
10
String inpu tl=JO pti
onPan
e. show
Inpu tDialog(z,please input first nu mber:〃);
String inpu t2=JO pti
onPan
e. show
Inpu tDialog (,z please input sec ond nu mber:〃); try (
int
nl=ln teger.
parse
In t (i
npu tl);
int
n2=ln teger.
parse
In t (i
npu t2);
int
div=nl/n2;
10
JOp
tio nPan e. showMessageDialog( null/吆吉 果 ;^〃+div,丝吉
〃,JO ptio nPan e. );
)
catch(NumberFormatExce ptio n e){
JOp ti onPan e. showMessageDialog( null, PI ease input nu meric digits!”,
〃,JO ptio nPan e. );
)
catch(ArithmeticExce pti on e){
10
10
JOp tio nPan e. showMessageDialog( null, z,Divisio n
by
zero!","
10
〃,J LAIN_MESSAGE);
10
System, exit(0);
)
)
⑵实验结果:
8
10
10
结果
.计算n并捕获可能出现的异常。 ⑴源程序:
public class syl0_2{
ublic static double multi (int n){
if (nvO) throw new 11 legalArgumentException(/Z^Jf 负异常〃);double s=l;
for (i nt i=l;i<=n ;i++) return s;
ublic static void main( Stri ng args[]) {
try{
int n=ln teger. parse In t (args[0]); System. out .prin tl
)
catch (Array In dexOutOfB oun dsExce ptio n e) {
System, out. printin应该输入,个参数");
}
catch(NumberFormatExce pti on e2){
System, out. printing'参数应该为整数〃);
}
catch(IllegalArgume ntExce ptio n e3) {
System. out. printin (〃 出现* 的异常为:〃+e3. toString ()); ) fin ally{
System. out. printinC 11 -算阶结束/');
)
⑵实验结果:
Sei lain kethod
ZQ
11
H C:\Progia