异常
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<<arr.length;i++)
{
System.out.printlnarr[i]+" ");
}
}
catch(Exception e){
System.out.println("数组出现了问题");
}
}
}
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);
}
}