java基础_7_异常
发布日期:2021-05-15 00:26:31 浏览次数:23 分类:精选文章

本文共 2926 字,大约阅读时间需要 9 分钟。

异常

在这里插入图片描述
Exception
java.lang.NullPointerException
ClassCastException
Array
indexOutofBoundsException
java.lang.Object
____java.lang.throwable
_java.lang.error //硬伤
_java.lang.Exception //
Throwable:
可以抛出的
所有异常和错误的超类
图throw //抛出异常对象的指令
图throws //在方法中修饰抛出异常的关键字
在这里插入图片描述
异常细节
在这里插入图片描述
异常处理
在这里插入图片描述
throwable
在这里插入图片描述

class Demo8{ public static void main(String[]args) { int []arr=null; //System.out.println(arr.length);空指针输出会出错 Exception e2=new Exception("出错了"); //打印异常消息 System.out.println(e2.getMessage()); //打印栈跟踪信息   } //异常处理,检查错误 public static void test (){ try{ throw new Exception("出错了");	 }//需要检测的代码 catch(Exception e){ System.out.println(e.getMessage());//获取异常信息,返回字符串 	}//异常处理代码 finally{ System.out.println("搞定了"); }//一定会输出的代码,有没有都可以		}	}}
class Demo9{public static void main(String []args){int []arr={1,2,3,4,5};outArr(arr);}//输出数组public static void outArr(int [] arr){try{for(int i=0;i<
class Demo10{public static void main(String []args)throws AgeTooSmallException, AgeTooBigException//表示暴露异常{Person p=new Person();try{	p.setAge(201);}catch(Exception e){	System.out.println(e.getMessage());}System.out.println("over");}}class Person {private int age;//私有变量需要getset才能看到public int getAge(){return age;	}public void setAge(int age)throws AgeTooSmallException, AgeTooBigException//表示暴露异常{if(age<0){   throw new AgeTooSmallException("年龄过小");  }   if(age>200){   throw new AgeTooBigException("年龄过大");  }  this.age=age;	}}class AgeTooSmallException extends Exception{public AgeTooSmallException(){}public AgeTooSmallException(String mag){  super(mag); 	} } class AgeTooBigException extends Exception{public AgeTooBigException(){}public AgeTooBigException(String mag){  super(mag); } }
class Demo10{	public static void main(String[]args){	//outArr(arr);  Person p =new Person();  try{   p.setAge(201);  }  catch(AgeTooSmallException e){   System.out.println("年龄带小了");   }  catch(AgeTooBigException e){   System.out.println("年龄带大了");   }//细节的往前放   catch(Exception e){   System.out.println(e.getMessage());  }  System.out.println("over");  }}class Person {private int age;public int getAge(){return age;}public void setAge(int age)throws AgeTooSmallException, AgeTooBigException{if(age<0){throw new AgeTooSmallException("年龄过小");}if(age>200){throw new AgeTooBigException("年龄过大");}this.age.age;}}class Student extends Person{public void setAge(int age)throws AgeTooSmallException{if(age<6){throw new InvalidAgeException("超出范围");}super.setAge(age);System.out.println("设置年龄over");}}class AgeTooSmallException extends Exception{	public AgeTooSmallException(){}	public AgeTooSmallException(String mag){	super(mag);	}}class AgeTooBigException extends RuntimeException//对比Runtime和非runtime(与Small对比{public AgeTooBigException (){}public AgeTooBigexception(String mag){super(mag);}}//自定义异常class InvalidAgeException extends AgeTooSmallException{	private String info;	public InvalidAgeException (String info){	this.info=info;	}	public void outInfo(){	System.out.println(info);	}}
上一篇:LaTeX基本字体设置-2
下一篇:LaTeX下载安装-1

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2025年04月24日 04时49分06秒