异常1

来源:互联网 发布:程序员的数学pdf百度云 编辑:程序博客网 时间:2024/06/16 14:54
Throwable 是所有异常的超类 当对象是此类实例 错误才会被抛出
throw 抛出异常对象的指令
throws 在方法中声明抛出异常的关键字 
java.lang.Object
---java.lang.Throwable
------java.lang.Error 硬伤
------java.lang.Exception try catch finally 来解决
-----------------------------------------------------
try{
  需要检测的代码;
}catch(异常类 变量/Throwable e){
  异常处理代码
}
finally{
  一定要执行的代码
}
finally 代码块只有一种情况不被执行 之前执行System.exit(0)(jvm都关了)
-------------------
getMessage() 获取异常消息 返回字符串
toString() 获取异常类名和异常信息,返回字符串
printStackTrace() 获取异常类名和异常信息,以及异常在程序中出现的位置 返回值 void 死递归抛出无数个异常那个例子
printStackTrace(PrintStream s) 讲异常内容保存在日志文件中,以便查阅
--------------------
throws 用在函数上,后面跟异类名(声明异常 )
throw用在函数内 后面跟异常对象(抛出异常对象)
-----------------------------------------------------
class ExceptionDemo1{
  public static void main(String[] args){
    //int[] arr=null;
    //System.out.println(arr.length); //NullPointerException
    //int[] arr={1};
    //System.out.println(arr[1]);//ArraOfIndexOutOfBoundsException
    //Exception e = new Exception();
    //Exception e2 = new Exception("Error");
    //print(e2.getMessage()); //getMessage方法是继承Throwable传下来的
    //e2.printStackTrace(); Exception没有自己的方法 全是继承Throwable
    //print(1/0);  //ArithmeticException


    //throw e2;   //抛出异常 上面一行已经挂了 编译错误
    test(); 
  }


  public static void test(){
    try{
      throw new Exception("error");
    }


    catch(Exception e){
        print(e.getMessage());
    }
    finally{
        print("finished");
    }
}
---------------------------------------------------------
class ExceptionDemo2{
  public static void main(String[] args){
    int [] arr ={1,2,3};
    //int[] arr=null;  //arr.length 异常
    outArr(arr);
  }


  public static void outArr(int[] arr){
    try{
      for(int i=0; i<arr.length; i++){
        print(arr[i]+" ");
      }
    }
    catch(Exception e){
      print("array error");
    }


    //finally可以不用加
  }
}


------------------------------------------------------------
//自定义异常
class ExceptionDemo2{
  public static void main(String[] args){
    int [] arr ={1,2,3};
    //int[] arr=null;  //arr.length 异常
    outArr(arr);
  }


class Person{
  private int age;
  public int getAge(){
    return age;
  }


  public void setAge(int age) throws AgeTooSmallException, AgeTooBigException{
    if(age<0){
      throw new AgeTooSmallException("too young"); //要new 一个异常
    }
    if(age>200){
      throw new AgeTooBigException("too old");  //未报告异常错误 要声明 报告 抛出
    }
    this.age=age;
    this.age = age;
  }
// 方法会往外抛异常 就要声明 捕获 所以要加throws  
}
//构造函数带参数了 上面AgeTooSmallException("too young")
class AgeTooSmallException extends Exception{
  public AgeTooSmallException(){
  
  }
  public AgeTooSmallException(String msg){
    super(msg);
  }


}


class AgeTooBigException extends Exception{
  public AgeTooBigException(){
  
  }
  public AgeTooBigException(String msg){
    super(msg);
  }
}


class ExceptionDemo3{
  public static void main(String[] args)  //1. throws AgeTooSmallException, AgeTooBigException{
    Person p = new Person();
    2.//try{    
      p.setAge(100); //setAge 存在异常 所以要对风险作出处理 要么main 抛出异常 要么 try catch捕获
      
    }
    catch (Exception e){
      print(e.getMessage());
    }
    print();


  }
}
--------------------------
Throwable