欢迎来到天天文库
浏览记录
ID:21935705
大小:244.50 KB
页数:27页
时间:2018-10-21
《java速成课件异常2》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、Exception子类的继承关系ExceptionClassNotFoundExceptionClassNotSupportedExceptionIllegalAccessExceptionInstantiationExceptionInterruptedExceptionNoSuchMethodExceptionRuntimeExceptionArithmeticExceptionArrayStoreExceptionClassCastExceptionIllegalArgumentExceptionException子类(续)IllegalThread
2、StateExceptionNumberFormatExceptionIllegalMonitorStateExceptionIndexOutOfBoundsExceptionArrayIndexOutOfBoundsExceptionStringIndexOutOfBoundsExceptionNegativeArraySizeExceptionNullPointerExceptionSecurityException13周-上机作业1设计一个java程序,有数组a()={10,20,30,40,50};b()={0,2,30};求数组c()=a[i]/
3、b[i],i=0-4.请处理此程序所发生的任何异常(ArrayIndexOutofBoundsException)(ArithemeticException)1关于异常的含义,下列描述正确的是A程序编译错误B程序语法错误C程序自定义的异常D程序编译或者运行时发生的异常事件D5对于try和catch子句的排列方式,下列哪一项是正确的?A子类异常在前,父类异常在后B父类异常在前,子类异常在后C只能有子类异常D父类异常和子类异常不能同时出现在同一个类中A7关于java语言中常用的异常类的说法,错误的一个是A在不合法的数学运算发生时,会出现ArithmeticEx
4、ception类异常B如果字符串转换成数字,需要准备处理NumberFormatException异常C如果使用非法的索引值来访问数组,ArrayIndexOutOfBoundsException异常会被抛出D如果指针变量为空,NullPointException异常就会被抛出DJava中没有指针使用null对象引用方法或变量将产生该异常14运行下列程序,将产生什么异常?AArrayIndexOutOfBoundsExceptionBNumberFormatExceptionCArithmeticExceptionDArithmeticException和
5、ArrayIndexOutOfBoundsExceptionpublicclasstest14{publicstaticvoidmain(String[]args){intx=0;inty=2/x;intz[]={1,2,4,6};intp=z[4];}}C抛出异常语句通常情况下,异常是由系统自动捕获的。但程序员也可以自己通过throw语句抛出异常。throw语句的格式为:thrownew异常类名(信息)其中异常类名为系统异常类名或用户自定义的异常类名,“信息”是可选信息。如果提供了该信息,toString()方法的返回值中将增加该信息内容。publicc
6、lassDeposit{publicstaticdoublediscount(intn){if(n<0){thrownewIllegalArgumentException("n应该为正整数");}returnn*0.08;}publicstaticvoidmain(Stringargs[]){try{inti=Integer.parseInt(args[0]);System.out.println("用户消费"+i+"元,可优惠"+discount(i)+"元");}catch(ArrayIndexOutOfBoundsExceptione1){Syste
7、m.out.println("异常:没有输入参数");}catch(NumberFormatExceptione2){System.out.println("异常:参数不能转换为整型");}catch(IllegalArgumentExceptione3){System.out.println("自定义异常:"+e3.toString());}finally{System.out.println("程序结束");}}}【任务8-3】抛出异常注意异常类型的捕获顺序异常:没有输入参数程序结束异常:参数不能转换为整型程序结束自定义异常:java.lang.Ill
8、egalArgumentException:n应该为正整数程序结束
此文档下载收益归作者所有