异常

1. 什么是异常

JAVA程序在运行时期出现的不正常。
JAVA 对这种现象,异常进行了对象封装。

类:是对对象抽象描述
对象:确实存在对实体

异常描述:
nullpointerexception 描述空指针异常对象
classnotfoundexception 类型转换异常
arithmeticexception 数字计算异常
arrayindexoutofboundsexception 描述数组索引越界异常
StringIndexOutBounds 字符串越界异常
IndexOutBounds 越界异常,出现在集合中

2. Java异常类中对继承体系

Java中一切异常和错误对=的父类 java.lang.Throwable
Throwable:
 |--Error 错误;程序中出现了非常严重的问题,不修改代码运行不了
 |--Exception 异常;程序中出现的轻微的问题,处理之后,可以继续执行
   |--RunTimeException 运行时期异常
   |--非RunTimeException ...

Throwable构造方法:
  空参数构造方法
  传递字符串:传递字符串类型的异常信息

Throwable普通方法(3个):
  String toString() 返回异常信息的简短描述
  String getMessage() 返回异常信息的详细描述
  void pringStackTrace() 异常信息追踪到标准输出流

/*
 * 异常的演示
 */
public class ExceptionDemo {

    public static void main(String[] args) {
        int result = div(3, 0);
        System.out.println(result);

    }
/*
 * 演示除数为0的异常
 * 定义方法,计算两个数的除法
 */
    public static int div(int a, int b) {
        return a / b;
    }

}
数字计算异常信息
异常_第1张图片
异常的JVM处理过程
/*
 * 异常的演示
 */
public class ExceptionDemo1 {

    public static void main(String[] args) {
        int[] arr = {};
        array(arr);

    }
    // 数组越界异常,注意:⚠️0角标同样可能越界,当定义一个长度为0的数组
    public static void array(int arr[]) {
        System.out.println(arr[0]);

    }

}
数组角标越界异常

3. 异常类的处理方式

try...catch
抛出异常

package ecn.it;

/*
 * 异常的第一种处理方式
 * try{
 *  可能发生异常的代码
 *  检测用的
 * }catch(异常类名 变量){
 *  异常处理方式
 *  直接输出异常信息
 *  写循环,判断,调用方法
 * }
 */
public class ExceptionDemo1 {
    public static void main(String[] args) {
        try {
            // 可能发生异常的代码
            int result = div(3, 0);
            System.out.println(result);
            System.out.println("aaa");
        } catch (Exception e) {
            // 异常处理代码
            System.out.println("toString:"+e);
            System.out.println("getMessage:"+e.getMessage());
            System.out.println("====");
//使用最多
            e.printStackTrace();
        }
        System.out.println("bbb");

    }

    public static int div(int a, int b) {
        return a / b;
    }
}

异常_第2张图片
使用try...catch处理的结果,另:throwable的三个方法
异常_第3张图片
try...catch异常处理过程

如何处理异常

选择  try... catch...  或者throws

  如果写一个功能,自己不用,交给别人去使用,如果有异常,请你抛出,不要处理
  如果使用的是别人已经做好的功能,如果有异常,请你try catc

4. 编译和运行异常

当调用了抛出异常的方法,如果不对方法处理,编译失败
处理方式,可以try catch 继续throws

凡是RuntimeException的子类,或者RuntimeException自己
都是运行时期的异常:
方法内部抛出的异常,是运行时期异常,不需要throws声明出来,调用者也不需要处理
throw new RuntimeException或者是他的子类
方法声明上,不用throws

对于运行时期异常,为什么要这样设计
运行时期异常一旦发生,请你不要处理,必须停下来,修改源代码
运行时期异常,根本就不能发生

RuntimeException 继承 Exception
  子: NullPointerException
  子:ArrayIndexOutOfBoundsException
  子:StringIndexOutOfBoundsException
  子: IndexOutOfBoundsExceptionException
  子: ClassCastException

方法的调用上,如果用户乱传递参数,导致程序无法计算,应该使用运行时期异常

/*
 * 运行时异常案例
 *
 *  设计一个方法,查找一个字符在字符数组中第一次出现的索引
 *  数组,和被查找的字符,参数传递的形式
 *  
 *  定义方法,计算圆形的面积
 *    半径平方*圆周率
 */
public class ExceptionDemo7 {
    public static void main(String[] args) {
        /*char[] ch = {'a','b','q','s','w','t'};
        
        int index = search(new char[0],'D');
        System.out.println(index);
        if(index < 0 ){
            System.out.println("没有");
        }else{
            System.out.println("有了,索引是 "+index);
        }*/
        double d = 0;
        d = getArea(-1);
        System.out.println(d);
        
    }
    //定义方法,传递半径,计算圆周率
    public static double getArea(double r){
        if(r <= 0)
            throw new RuntimeException("半径非法");
        return r*r*Math.PI;
    }
    
    
    //定义方法,传递数组,和查找的关键字,返回出现的索引
    public static int search(char[] ch, char key){
        //对数组进行非空判断
        if(ch == null)
            throw new RuntimeException("数组不存在");
        if(ch.length==0)
            throw new RuntimeException("数组不存在");
        for(int x = 0 ; x < ch.length ; x++){
            if(ch[x] == key)
                return x;
        }
        return -3;
    }
}

5. throw和throws

 throw 方法内部,手动抛出异常,后面跟随异常对象
 throws 方法声明,表明方法抛出异常,请调用者处理,后面写异常类的名字
   如果多个异常类,逗号分开

6. final finally finalize

 final 修饰符,类,方法,变量
  finally 异常处理,必须执行的代码
  作用:释放资源使用
  finalize 对象变成垃圾的时候,JVM收取的时候调用

7. 子类父类异常处理

/* 
 * 子类继承父类,重写父类的方法,异常怎么处理
 *   父类的方法,抛出异常,子类重写了,异常怎么处理
 *     父类抛出异常,子类可以抛,可以不抛
 *     如果子类抛,抛出的异常,不能大于父类抛出的异常 (异常的继承关系)
 *   
 *   父类的方法,不抛出异常
 *     子类重写后,不能抛出异常,父类不抛,子类不能抛
 *     子类调用了一个抛出异常的方法,子类只能try catch
 *     
 *  实际项目中个,尽量不要使用带有子类父类关系的异常
 */
class AException extends Exception{}
class BException extends AException{}
class CException extends Exception{}
class Fu{
    public void show() {
        
    }
}
class Zi extends Fu{
    public void show(){
        try {
            method();
        } catch (Exception e) {
            
            e.printStackTrace();
        }
    }
    
    public void method()throws Exception{
        
    }
}

public class ExceptionDemo9 {
    public static void main(String[] args) {
    
        //f.show();
    }
}

你可能感兴趣的:(异常)