当前位置:首页 > Java实验指导书44-49
作 业
一、作业内容(包括作业题目和解答)
1、 该程序能处理多种异常。
publicclassManyException { } }
System.out.println(\继续!!\); }
}
publicstaticvoid main(String[] args) {
inti;
inta[]={1,2,3,4}; for(i=0;i<5;i++){
try { }
catch(ArrayIndexOutOfBoundsExceptione) {
System.out .print(\捕获到了数组下标越界异常\);
System.out.println(\+i+\+i+\+(a[i]/i));
} {
catch(ArithmeticExceptione)
System.out .print(\异常类名称是:\+e);
} {
catch(Exception e)
System.out .println(\捕获\+e.getMessage()+\异常!\); }
finally{
System.out.println(\+i);
2、 编写一个Java程序,这个程序定义了一个能抛出异常的方法,在main()方法中捕获并处理该异常。
3、 编写一个Java程序,这个程序有两个异常类,在main()方法中使用throw语句抛出异常。
classAaaExceptionextends Exception{} classBbbExceptionextends Exception{}
classThrowException { { { } }
System.out.println(\执行bbb异常处理程序\); }
System.out.println(\执行aaa异常处理程序\); }
publicstaticvoid main(String[] args) {
intx=1; try{
thrownewAaaException();
if(x>0) else
thrownewBbbException(); }
catch (AaaExceptione)
catch (BbbExceptione)
4、编写一个Java程序,在main()方法中通过键盘读入一个字符。
importjava.io.*;
publicclassCatchException { }
publicstaticvoid main(String[] args) {
System.out.println(\程序开始\); try { }
catch(Exception e){}
System.in.read();
System.out.println(\程序结束\); }
共分享92篇相关文档