认识异常,自定义异常

1、什么是异常?

异常就是程序运行过程中出现的问题

2、异常的体系:

Error:

代表的系统级别错误(属于严重问题),也就是说系统一旦出现问题,sun公司就会把这些问题封装成Error对象给出来

Exception:

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

*运行时异常:

RantimeException及其子类,编译阶段不会出现错误提醒,运行时出现的异常(如数组索引越界异常)

*编译时异常:

编译阶段就会出现错误提醒的。(如日期解析异常)

异常的处理方法:

①抛出异常(throws)

*在方法上使用throws关键字,可以将方法内部出现的异常抛出去给调用者处理。

方法  throws  异常1,异常2,异常3...{

        ...

}

捕获异常(try...catch)

*直接捕获程序出现的异常

try{

        //监视可能出现异常的代码;

}catch(异常类型1  变量){

        //处理异常

}catch(异常类型1  变量 ){

        //处理异常

}...

自定义异常:

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

自定义异常的种类

自定义运行时异常 自定义编译时异常

1、定义一个异常类来继承RuntimeException

2、重写构造器

3、通过throw new  异常类(xxx)来创建异常对象并抛出

编译阶段不报错,提醒不强烈,编译阶段,运行时才可能出现

1、定义一个异常类继承自Exception

2、重写构造器

3、通过throw new  异常类(xxx)来创建异常对象并抛出

4、编译阶段就报错,提醒更强烈

//目标:掌握自定义异常,以及异常的作用
public class YiChang {
    public static void main(String[] args) {
//保存一个合法的年龄
        try {
            saveAge(525);
            System.out.println("底层执行成功了");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("底层出现了bug");
        }
        try {
            saveAge1(25);
            System.out.println("底层执行成功了");
        } catch (ageilegalException e) {
            e.printStackTrace();
            System.out.println("底层出现了bug");
        }
    }
    public static void saveAge(int age){
        if (age>0&&age<150) {
            System.out.println("年龄合法");
        }else{
            //用一个异常对象来封装这个问题
            //throw抛出这个异常对象
        throw new ageilegalRuntimeException("/age is illeagal,your age is "+age);
        }
    }
    public static void saveAge1(int age)throws ageilegalException{
        if (age>0&&age<150) {
            System.out.println("年龄合法");
        }else{
            //用一个异常对象来封装这个问题
            //throw抛出这个异常对象
            //throws用在方法上,抛出方法内部的异常
            throw new ageilegalException("/age is illeagal,your age is "+age);
        }
    }
}
public class ageilegalRuntimeException extends RuntimeException{
    public ageilegalRuntimeException() {
    }

    public ageilegalRuntimeException(String message) {
        super(message);
    }
}
public class ageilegalException extends Exception{
    public ageilegalException() {
    }

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

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

1、捕获异常,记录异常,并相应合适的信息给用户。

2、捕获异常,尝试重新修复

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class YiChang1 {
    //目标:异常的处理
    /*
    public static void main(String[] args) {
        try {
            test();

        } catch (FileNotFoundException e) {//捕获异常
            System.out.println("你要找的异常信息是不存在的");
            e.printStackTrace();//打印异常信息,记录下来
        } catch (ParseException e) {
            System.out.println("你要解析的时间是不存在的");
            e.printStackTrace();
        }
    }
    public static void test() throws FileNotFoundException, ParseException {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
        Date date=simpleDateFormat.parse("2024-05-02 9:05:48");
        System.out.println(date);
        test2();
    }
    public static void test2() throws FileNotFoundException {
        //读取文件的
        InputStream is=new FileInputStream("D:/woainizhongguo");


    }
     */
    public static void main(String[] args) {
        try {
            test();
        } catch (Exception e) {//捕获异常
            System.out.println("你当前操作有问题");
            e.printStackTrace();
        }
    }
    public static void test() throws Exception, Exception {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
        Date date=simpleDateFormat.parse("2024-05-02 9:05:48");
        System.out.println(date);
        test2();
    }
    public static void test2() throws Exception {
        //读取文件的
        InputStream is=new FileInputStream("D:/woainizhongguo");


    }
}
import java.util.Scanner;

public class YiChang2 {
    public static void main(String[] args) {
        //需求:调用一个方法,让用户输入一个合适的价格
        while (true) {
            try {
                System.out.println(money());
                 break;
            } catch (Exception e) {
                System.out.println("请您输入合法的价格");
            }
        }

    }
    public static double money(){
        Scanner sc=new Scanner(System.in);
        while (true) {
            System.out.println("请您输入合适的价格");
            double money=sc.nextDouble();
            if (money>=0) {
                return money;
            }else{
                System.out.println("您输入的价格是不合适的");

            }
        }
    }
}

相关推荐

  1. 认识异常定义异常

    2024-05-02 19:46:05       26 阅读
  2. 定义异常

    2024-05-02 19:46:05       23 阅读
  3. Spring Security OAuth2 认证服务器定义异常处理

    2024-05-02 19:46:05       59 阅读
  4. springboot全局异常处理和定义异常处理

    2024-05-02 19:46:05       66 阅读
  5. SpringBoot实现定义异常+全局异常统一处理

    2024-05-02 19:46:05       55 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-02 19:46:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-02 19:46:05       100 阅读
  3. 在Django里面运行非项目文件

    2024-05-02 19:46:05       82 阅读
  4. Python语言-面向对象

    2024-05-02 19:46:05       91 阅读

热门阅读

  1. 创建并管理Python虚拟环境:深入理解venv

    2024-05-02 19:46:05       25 阅读
  2. python 关键字(import)

    2024-05-02 19:46:05       31 阅读
  3. Spring中控制反转究竟反转的什么

    2024-05-02 19:46:05       25 阅读
  4. DNS解析过程

    2024-05-02 19:46:05       37 阅读
  5. 亲子公园实景剧本杀小程序系统开发

    2024-05-02 19:46:05       31 阅读
  6. C# 程序启动另外一个exe的时候传参数

    2024-05-02 19:46:05       34 阅读
  7. 图搜索算法详解

    2024-05-02 19:46:05       30 阅读
  8. 【AIGC半月报】AIGC大模型启元:2024.04(下)

    2024-05-02 19:46:05       33 阅读
  9. 如何在前端展示后端返回的pdf Base64格式字符串

    2024-05-02 19:46:05       27 阅读
  10. 第二弹:走进CSS世界,学习记录

    2024-05-02 19:46:05       29 阅读
  11. 【C++】循环语句中引起的循环引用问题

    2024-05-02 19:46:05       37 阅读
  12. npm详解

    2024-05-02 19:46:05       39 阅读
  13. C++可变参数模板中的省略号

    2024-05-02 19:46:05       33 阅读