异常Exception

什么是异常?

异常就是代表程序出现的问题

Error:代表的系统级别错误(属于严重问题),也就是说系统一旦出现问题,sun公司会把这些问题封装成Error对象给出来,说白了,Error是给sun公司自己用的,不是给程序员用的,因此开发人员不用管他。

Exception:叫异常,他代表的才是程序可能出现的问题,所以,程序员通常会用Exception以及它的孩子来封装程序出现的问题。

  • 运行时异常:RuntimeException及其子类,编译阶段不会出现错误提醒,运行时出现的异常(如数组索引越界异常)
  • 编译时异常:编译阶段就会出现的错误提醒(如:日期解析异常)

异常有什么作用?

1.异常是引来查询系统Bug的关键参考信息

2.异常可以作为方法内部的一种特殊返回值,以便通知上层调用者底层的执行情况

 

public class test {
    public static void main(String[] args) {
        try {
            System.out.println(divide(10,0));
            System.out.println("成功");
        }
        catch (Exception e){//如果调用divide抛出异常,这里就会捕获异常并输出异常信息
            System.out.println("出错了");
            e.printStackTrace();
        }
    }
    public static int divide(int a,int b){
        if(b==0){
            System.out.println("参数有问题");
            // 抛出运行时异常,自定义输出的异常信息,通知上层调用者
            throw new RuntimeException("参数有问题");
        }
        int c=a/b;
        return c;
    }
}


输出结果:
参数有问题
出错了
java.lang.RuntimeException: 参数有问题
	at test.divide(test.java:18)
	at test.main(test.java:6)

自定义异常

  • Java无法为这个世界上所有的问题都提供异常类来代表,如果企业自己的某些问题想通过异常来表示,以便用异常来管理该类问题,就需要自定义异常类。

自定义异常类的种类

自定义运行时异常:

  • 定义一个异常类继承RuntimeException.
  • 重写构造器
  • 通过throw new异常类(xxx)来创建异常对象并抛出。编译阶段不报错,提醒不强烈,运行时才有可能出现。
    
    public class test {
        public static void main(String[] args) {
            try {
                save(250);
                System.out.println("成功");
            }
            catch (Exception e){
                System.out.println("出错了");
                e.printStackTrace();
            }
        }
        public static void save(int age) throws exception.AgeIlleagalRuntimeException {
            if(age<=0||age>150){
                System.out.println("参数有问题");
                // 这个年龄非法,创建自定义异常对象并抛出
                throw new exception.AgeIlleagalRuntimeException("参数有问题");
            }
            System.out.println("年龄保存成功,是"+age);
        }
    
    
    }
    
    public class exception {
        //自定义运行时异常
        //1.继承RuntimeException
        //2.重写构造方法
        public static class AgeIlleagalRuntimeException extends RuntimeException{
            public AgeIlleagalRuntimeException(){
            }
            public AgeIlleagalRuntimeException(String message){
                super(message);
            }
        }
    }
    

    自定义编译时异常:(由于耦合性太强,导致一个异常会引起以上的层级都出现报错,不建议使用)

  • 定义一个异常类继承Exception
  • 重写构造器
  • 通过throw new异常类(xxx)来创建异常对象并抛出。编译阶段报错,提醒更加强烈。
    
    public class test {
        public static void main(String[] args) {
            try {
                save(250);
                System.out.println("成功");
            }
            catch (Exception e){
                System.out.println("出错了");
                e.printStackTrace();
            }
        }
        public static void save(int age) throws exception.AgeIlleagalException {
            //throw:方法内部使用,创建异常并从此点抛出去
            //throws:方法上,抛出方法内部的异常给调用者
            if(age<=0||age>150){
                System.out.println("参数有问题");
                // 这个年龄非法,创建自定义异常对象并抛出
                throw new exception.AgeIlleagalException("参数有问题");
            }
            System.out.println("年龄保存成功,是"+age);
        }
    
    
    }
    
    public class exception {
        //自定义编译时异常
        //1.继承RuntimeException
        //2.重写构造方法
        public static class AgeIlleagalException extends Exception{
            public AgeIlleagalException(){
            }
            public AgeIlleagalException(String message){
                super(message);
            }
        }
    }
    

    throw和throws方法的区别:

    throw:方法内部使用,创建异常并从此点抛出去

    throws:方法上,抛出方法内部的异常给调用者

开发中对于异常的常见处理方式 

1.捕获异常,记录异常并响应给合适的信息给用户

import javax.xml.crypto.Data;
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class test {
    public static void main(String[] args) {
        try {
            parseData("2023-11-11 11:11:11");
            System.out.println("成功");
        }
        catch (Exception e){
            System.out.println("出错了");
            e.printStackTrace();
        }
    }
    public static void parseData(String s) throws Exception {//将异常1、2都统一抛出去
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d=sdf.parse(s);//编译时异常,写代码时就报错 异常1
        System.out.println(d);
        InputStream is=new FileInputStream("");//异常2
    }

}

2.捕获异常,尝试修复


public class test {
    public static void main(String[] args) {
       while(true){
           try {
               double price = getprice();
               System.out.println("商品的价格是"+price);
               break;
           } catch (Exception e) {//这里用户如果输入的不是double类型的就会报错并终止程序
               //catch就会捕获这个异常并输出提示
               //这里有while循环就会重新让用户输入直到输入一个合法的数值,break跳出while循环
               System.out.println("输入的形式不合法");
           }
       }
    }
    public static double getprice() {
       Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个合法的价格");
        double price = scanner.nextDouble();
        return price;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值