异常 数组 10 |
您所在的位置:网站首页 › 数组异常 › 异常 数组 10 |
一、异常(Throwable)
1.理解 Throwable--------->Error||Exception 异常:程序无法正常执行完毕。 异常的文类:运行时|检查时 2.Error Error:这类不需要程序员关注,一般是由虚拟机生成并脱出的 3.Exception(异常) 1)RuntimeException:运行时异常,运行期间发生的异常,编译时没有问题 可以通过增强程序的健壮性来处理 if..else 常见的运行时异常: 空指针异常:NullPointerException :当对象无法正常存在使用,会发生空指针 类型转换异常: ClassCastException -->intansceof 数组下标越界 数组长度负数异常 数学异常 :ArithmeticException 数字转换异常: NumberFormatException 2)CheckedException:检查时异常|编译时异常:发生在编译期间 如果不处理,程序无法运行。 4、异常处理 1)throws:异常抛出,抛给上一层处理,当前不处理 2)try......catch 异常捕获 try{ 可能会出现异常的代码; }catch(NullPointerException e){ //捕获空指针类型的对象,或者多态 如果出现这个异常,执行的代码... }catch(FileNotFoundException e){ 如果出现这个异常,执行的代码... }.... catch(Exception e){ 任意类型的异常都可以捕获 }finally{ 无论try中是否出现异常,都会执行finally中的代码 }try一旦弄到异常,try后面的代码不执行,直接去找对应得catch,如果后面有很多catch,就依次从上到下判断。一个try后面可以跟多个catch,至少一个catch。 如果发现多个catch,捕获异常类型范围要从小到大。 public class Exception04 { public static void main(String[] args) { // TODO Auto-generated method stub try { System.out.println("开始。。。。。。。。。。"); int i=5/0;//数子异常 String str=null;//空指针异常 System.out.println(str); FileInputStream file=new FileInputStream("F://bin");//文件找不到异常 }catch(ArithmeticException e) { //打印出来 System.out.println("数字异常。。。。"); e.printStackTrace(); System.out.println(e.getMessage()); }catch(NullPointerException e) { System.out.println("空指针。。。。"); e.printStackTrace(); }catch(FileNotFoundException e) { System.out.println("文件找不到。。。。。"); e.printStackTrace(); }catch(Exception e) { e.printStackTrace(); System.out.println("异常处理。。。"); }finally { System.out.println("一般资源最好的地方放这里"); } System.out.println("结束。。。。。。。。"); } }
5.自定义异常 throw 制造异常 前提:异常对象的类型需要继承自Throwable 自定义异常: 直接或者间接的继承自Exception 运行时异常: 继承自RuntimeException 编译时异常: Exception或者某一个编译时异常的子类 /** 编译时异常:密码要大于等于6位数 */ public class Exception05 { public static void main(String[] args){ // TODO Auto-generated method stub Info in=new Info(); String name="xiaoxiao"; String pwd="123"; in.setUname(name); try { in.setUpwd(pwd); } catch (StringException e) { // TODO Auto-generated catch block try { in.setUpwd("123456"); } catch (StringException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } e.printStackTrace(); } System.out.println(in.toString()); } } class Info{ private String uname; private String upwd; //setter 和 getter 访问器 public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getUpwd() { return upwd; } public void setUpwd(String upwd) throws StringException { if(upwd.length()>=6) { this.upwd = upwd; }else { throw new StringException(upwd+"密码要大于六位"); } } @Override public String toString() { return "Info [uname=" + uname + ", upwd=" + upwd + "]"; } } //定义编译异常 class StringException extends Exception{ public StringException() { //super(); } public StringException(String mesgg) { super(mesgg); } } /** 运行异常:密码大于等于6位数 */ public class Exception06 { public static void main(String[] args){ // TODO Auto-generated method stub Info2 in=new Info2(); String name="xiaoxiao"; String pwd="123"; in.setUname(name); if(pwd.length()>=6) { in.setUpwd(pwd); }else { in.setUpwd("123456"); } System.out.println(in.toString()); } } class Info2{ private String uname; private String upwd; //setter 和 getter 访问器 public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getUpwd() { return upwd; } public void setUpwd(String upwd){ if(upwd.length()>=6) { this.upwd = upwd; }else { throw new StringException(upwd+"密码要大于六位"); } } @Override public String toString() { return "Info [uname=" + uname + ", upwd=" + upwd + "]"; } } //定义运行异常 class StringException extends RuntimeException{ public StringException() { //super(); } public StringException(String mesgg) { super(mesgg); } } 二、数组(Array)1.理解 1)数组:存储多个数据 2)在内存中是一段连续的内存空间 3)相同数据类型数据的有序集合 2.特点 数组是一个容器,是一个引用数据类型,堆中 数组中的所有数据要求数据类型相同 长度一旦确定不可改变 有序 根据序号使用(索引|下标) 3.数组声明 数据类型 [] 数组名; 数据类型:数组所有数据的数据类型 []------> 数组名:标识符 //定义静态数组初始化 int [] arr=new int[] {1,2,3,4,5,6,7,8,9,10}; //定义动态数组初始化 int [] arr2=new int[10];4.数组遍历 数组的遍历:依次的拿出数据的中每一个数据操作 1.for普通for循环 2.增强for循环:for..each for(数据类型 变量名 : 数组名|容器名){ 变量名 ->代表数组中的从前到后的每一个数据 } //增强for for(int i:arr) { //System.out.println(i); } System.out.println(Arrays.toString(arr)); //普通for for(int i=0;i |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |