Java异常

1.异常处理概述:

1.异常:

指的是 在程序的执行过程中 ,出现非正常的情况 ,最终会导致JVM非正常停止
Throwable类 是java 语言中 所有错误Error 或 异常Exception 的父类

        异常Exception

                编译异常 ,进行编译代码 (写代码) java程序 出现的问题

                RuntimeException: 运行异常 java运行过程中的异常

    error:错误

1.见过的RuntimeException 运行时异常·:

                1.空指针异常 NullPointerException(空对象 null 调用了方法 或者访问了属性)

                2.索引越界 ArrayIndexOutOfBoundsException( 访问数组 的索引越界 了 超过了最大索引值或者小于零)

                3.数字转换异常  NumberFormatException (字符串转数字 ,转换失败)

                4.算数除零异常 ArithmeticException (除数为零 或者 除不尽 其他算数 异常)

                5.栈溢出 StackOverflowError

                6.类型转换 异常 ClassCastException (不是 父子 或 实现关系 比如:猫不能转换成狗类)

等等。。。

2. 编译时异常:

        特点: 程序没运行 ,编译不通过 ,报异常      必须处理掉

  比如:

public class Demo {
//                                    在方法声明中 也可以 throws ParesException  
    public static void main(String[] args) /*throws ParseException*/ {

//        日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        sdf.parse("1999-7-26");
//        下面代码可以解决异常
        /*try {
//        这个代码回报异常
            sdf.parse("1999-7-26");

        } catch (ParseException e) {
            e.printStackTrace();
        }*/

        System.out.println("哈哈哈");

    }
}

注意:throws

        只是声明异常  写在方法声明上

作用;

        1.告诉调用者该方法有可能有异常

        2.只让编译期异常通过,不让他报红;

3.throw 手动 抛异常:

/*
* throw 手动 抛异常
*
* throw new  XXXException
*       以前 : Jvm基于运行 时的转态,创建 对应的异常对象
*       现在:手动 创建 异常对象
*     使用场景 : 结合自定义 的异常对象 ,手动抛出
*               JVM不认识 自定义 的异常 ,所以需要手动抛
* throws :
*       声明异常 , 主要编译期异常 让代码通过编译
*       调用者 ,要么 也得声明,真是出现异常 一样会将异常信息打印控制台
* throw:
*       手动造一个异常对象
*       写在方法体
*       结合自定义对象
*
*
* */
public class Demo {
    public static void main(String[] args) {
        throwE(false);
    }
    public static void throwE(boolean b){

        if(b){
//            抛异常
            throw new NullPointerException();
        }else {
            System.out.println("没有异常");
        }
    }
}

throw作用:

        1.和自定义异常结合使用;

        2.告诉调用者满足该条件下一定有抛的异常

4.*****try catch ***** 

        例如:

/*
* try{
*   有可能出现的代码段
* }catch(要捕获的异常类 变量){
*       捕获到了 会执行的代码
* }
* 后续代码
* */
public class Demo {
    public static void main(String[] args) {
        String str = null;

        try {
//            有可能出现异常的代码段
            str.length();//JVM 会创建一个 NullPointerException对象
            System.out.println(1);
        }catch (NullPointerException e){ // e = NullPointerException()
            //捕获到了
            System.out.println("异常捕获到了!");
            System.out.println(2);
        }

        System.out.println("后续代码...");
        System.out.println(3);
    }
}
*流程:******
*   1.先执行 try 里代码
*       1.1 没有异常:
*           跳过 catch  继续执行后续代码
*       1.2 有异常
*           匹配catch里的异常类
*               1.2.1如果匹配上:
*                   执行catch 后的代码
*                   异常处理成功了 后续代码会执行
*               1.2.2如果匹配不上:
*                   说明异常并没有处理,后续代码不执行

这里只有一个异常:那多个异常呢?

/*
* ctrl + alt + t   idea创建try catch 快捷键
* 多个异常 如何捕获
* */
/*
* 注意事项:
*   捕捉多个异常类
*       可以并行去写catch即可
*           比如:
*               catch(NumberFormatException e){}
*               catch(){}
*       注意: 如果子父类 将子类放前边
* 多个catch
*
* */
public class Demo02 {
    public static void main(String[] args) {

        try {
            int[] arr = {11,22,33};
            System.out.println(arr[5]);//异常
            System.out.println(1);//不会执行

            String str = "abf";
            Integer.parseInt(str);//异常
            System.out.println("try后代码");//不会执行
//            进行匹配
        } catch (ArrayIndexOutOfBoundsException e) {
//            e.printStackTrace();
            System.out.println("捕获到了 索引越界 异常");
//            再次进行匹配
        } catch (NumberFormatException e){
            System.out.println("捕获到 数字类型 异常");
        }

        System.out.println("程序正常结束");



        int[] arr = {11,22,33};
        String str = null;

        try {
            System.out.println(arr[7]);
            str.length();
        } catch (Exception e) {
//            e.printStackTrace();
            System.out.println("异常捕获到了");
        }
    }
}

注意:throws 和 try catch 区别:

/*
* throws和 try catch 都可以 处理编译 异常
*   throws : 只是声明 并没有 真正处理‘
*           调用者 还需处理
*   try catch:
*
*
* */
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class Demo {
    public static void main(String[] args) {

        method2();
        try {
            method1();
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }
//    处理 编译异常 方式一:
//    声明: 声明一个异常
    public static void method1() throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        sdf.parse("1999-11-00");

    }

    
//    方式二: 捕获
    public static void method2(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        try {
            sdf.parse("2000-55-66");

        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

简单地说:就是 这两都可以 处理编译 异常

        throws: 只是声明 并没有真正处理;

                        调用者 还要处理;

        try catch 能处理

5.Throwable(异常和错误的老大)成员方法:

方法名说明
public String getMessage()返回此 throwable 的详细消息字符串
public String toString()返回此可抛出的简短描述
public void printStackTrace()把异常的错误信息输出在控制台

1.

 printStackTrace() ; 打印异常信息  (不需要输出语句)
*                       直接打印 异常类名
*                               异常原因
*                               异常位置
public class Demo {
    public static void main(String[] args) {

        try {
//            String str = null;
//            str.length();

            int a = 1/ 0;//异常
        } catch (Exception e) {
            //把异常控制信息输出控制台  自己打印在控制台  ( 打印异常的完整信息)
              e.printStackTrace();
        }

        System.out.println("hhh");//能够输出
    }
}

 2.getMessage() ; 异常原因 黑色字体

public class Demo {
    public static void main(String[] args) {

        try {
//            String str = null;
//            str.length();
            int a = 1/ 0;
        } catch (Exception e) {
//            得到异常原因
            String message = e.getMessage();//   / by zero
            System.out.println(message);
        }

        System.out.println("hhh");
    }
}

 3.toString() ; 简短描述 黑色字体 *                 包括 异常类名 异常原因

public class Demo {
    public static void main(String[] args) {

        try {
//            String str = null;
//            str.length();
            int a = 1/ 0;
        } catch (Exception e) {

            String s = e.toString();//java.lang.ArithmeticException: / by zero
            System.out.println(s);
        }

        System.out.println("hhh");
    }
}

6.自定义异常:

/*
* 继承 RuntimeException
*       该类 是运行时 异常类
*  继承 Exception
*       该类 是 编译期异常
*
*
* */
public class AgeOutOfBoundsException extends RuntimeException{
//    AgeOutOfBoundsException 自定义异常

// 构造方法
    public AgeOutOfBoundsException() {
    }

    public AgeOutOfBoundsException(String message) {
        super(message);
    }

    public AgeOutOfBoundsException(String message, Throwable cause) {
        super(message, cause);
    }

    public AgeOutOfBoundsException(Throwable cause) {
        super(cause);
    }

    public AgeOutOfBoundsException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

華同学.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值