java基础异常及常见包_第1页
java基础异常及常见包_第2页
java基础异常及常见包_第3页
java基础异常及常见包_第4页
java基础异常及常见包_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、partll异常及常见包 了解常见的异常异常说明exception异常层次结构的根类run timeexception许多java.lang异常的基类arithmeticexception算术错误情形,如以零作除数illegalargumentexception方法接收到非法参数arraylndexoutofboundexception数组犬小小于或大于实际的数组犬小n u 11 po i n t ere xcept i o n尝试访问null对象成员classnotfou ndexception不能加载所需的类numberformatexception数字轻化格式异常,比如字符串钊float

2、 型数字的转择无轴ioexceptioni/o异常的根类filenotfoundexcepti on找不到文件eofexception文件结束i nterru pted e xcepti o n线程中断型匚從解决异常的方式:()用try catch和finally语句 语句结构:如下:try 需要排除异常的代码catch(exception e)/ exception e异常处理代码 finally程序结束前的执行的代码案例应用:源代码如下:package com.workitparti;publie class demol / * * param args/public static voi

3、d main(string args) / todo auto-generated method stubdemol div = new demol();tryint temp = div.div(10, 0 );system, out. print in (11 temp = ”+t emp);catch(arithmeticexception e)system.out.printin (n除数不能为零!输入错误! ! ”);system, out .printin (e . tostring () ) ; /输出错误处finallysystem, out. printin ( ”n=程序结

4、束=“); public int div (int first, int second)int result = 0;result = first/second;return result;多重try catch的应用(根类在前时就捕获不到在后了类的错误)案例应用: 源代码如下: package com.workit.parti;public class demo2 / * * param args*/public static void m=in(string args)/ todo auto-generated method stubdemo2 data = new demo2();str

5、ing str = (u10uz utn;datatest(str);public void test (string str) try try system.outprintin(=h+integer.parseint(str1 ); catch (arrayindexoutofboundsexception e) system, out .print in (u数组溢出,请重新输入! ! ”); e.printstacktrace ();catch (numberformatexception e) / todo auto-generated catch blocksystem, out

6、.printin (”输入错误!非字符型字符串无法转 化成整型! ! ! h + f,nf,+e.tostring();double datal = double.parsedouble(str0); double data3 = math.po(datal, 2 );system.out.printin(hdatal = n+datal);system, out. pri nt in (” datal 的平方为:n+data3); catch (numbeiformmtexception e) / todo auto-generated catch blocksystem. out. pri

7、nt in ("输入错误!输入的字符需为数字! ! ! n + ,fn,f+e . tostring ();finallysystem.out.printin colection 接口arraylist对彖相当丁是动态数组 案例应用:源代码如下:指定类型的package com.workit.parti;import java.util.arraylist;import java.util.list;public class demo3 / * param args*/public static void main(string args) / todo auto-genested

8、method stublist<string> list = new arraylist<string> (); list. add ( nlemon'1);list.add("tom”);list.add ( "rose");list add(n jack");for (int i = 0; idist size () ; i + +) system.outprintin (list.get(i) -tostring ();案例二:package comworkit.parti;public class student

9、private string name;private string stdid;private string origin;private int age;public student(string namez string stdid, string origin, int age) super();this .name = name;this.stdid = stdid;this < origin = origin;this.age = age;public string getname()return name;public string getstdid()return std

10、id;public string getorigin()return origin;public int getage()return age;package com.workit.parti;import java.util.arraylist;import javautillist;public class demo4 / * * param args/public static void main(string args) / todo auto-generated method stublist<student> student = new arraylist<stu

11、dent>();/装箱for (int i = 1;i<=100;i+)student add (new student ( nworkit,f+ iz ”num11 + i, nchiann z i); /拆箱system, out .printin (“姓名tt学号籍贯t年龄");for (int j = 0 ;j< student.size();j+)system. out. print in ( (n + s tuden t get ( j ) . getname () + n t ” + student.get(j) .getstdid()+ ht n +

12、 student get ( j) . getorigin () ) +,f tn + studentget ( j) .getage ();案例三:未指定类型的package com.workitparti;import javautil.arraylist;import javautil.list;public class demo5 / * * param args*/public static void main(string args) / todo auto-genetmted method stublist list = new arraylist ();list add(&qu

13、ot;lemon”);listadd(new integer (4);list.mdd(67);student std = new student ( htom,t, ,tworkit01,t, ,tusa,t z 21); listadd(std);for(int i = 0;i<list.size();i+)system.out.printin("list" + i + "=" +listget (i) .tostring ();student stdudent = (student)list.get(list.size()-1);system

14、, out. print in ("其中学生信息为:;system, out. printin (” 姓名 t 学号t 籍贯 t 年龄”);sys tern, out: print in (stdudent getname ()+ ,f=,f +stdudent getstdid () + ,r=n +stdudent getorigin () + ,r=n +stdudent getage (); hashmap 实现了 map 接口案例应用;源代码如:package com.workitpart 1;import java.util.hashmap;import javautil

15、-map;public class demo6 / * * param args*/public static void main(string args) / todo auto-generated method stubmap<string , student> studmap = new hashmap<string, student>(); for(int i = 0;i<10;i+)student stud = new student (nworkit fl + i, nnumfl + i, "usa j i); studmap.put (flstudentu + i, stud);s

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论