异常处理

异常处理

1、throwable类  (异常抛出)
   tostring()  返回该异常的名字和详细的字符串
   getMessage() 返回该异常的提示信息
   printStackTrace() 返回该异常的所有信息
2、try...Catch   (main方法中不用throwable,用try...catch处理)
   try{
        //需要被检测的语句
          }
    Catch(异常类 变量){
        //异常的处理语句
     }
    finally{
        //一定会被执行的语句
     }
public static void main(String[] args){
        try {
            fun(-1);
        } catch (Exception e) {
            //e.printStackTrace();
          //System.out.println(e.toString());
            System.out.println(e.getMessage());
        }finally{
            System.out.println("最终的语句");
        }
    }
    public static double fun(int i){
        if(i<0)
             throw new RuntimeException("元不存在");
              return i*i*Math.PI;
    }
}