异常处理。

本文详细介绍了Java中的异常处理机制,包括`throws`、`try/catch`的使用方式,以及在企业中如何优雅地处理异常。此外,还展示了如何通过自定义异常来增强代码的可读性和鲁棒性。最后,通过一个示例展示了如何从用户输入中验证合法价格,确保程序的正常运行。
摘要由CSDN通过智能技术生成

。。。。。。。。。。。。。 

1. throws

public static void main(String[] args) throws ParseException, FileNotFoundException {
        System.out.println("程序开始");
        ParseTime("2020-12-12 11:11:11");
    }
    //系统怕我们搞错格式,所以用parse需要加上throws ParseException来提醒我们仔细核对格式,并且调用该方法的方法也需要要加上
    //系统怕我们写错了路径所以创建FileInputStream对象需要加throws FileNotFoundException
    //throws Exceptoin能抛出所有异常,因为是父类
    private static void ParseTime(String s) throws ParseException, FileNotFoundException {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d=sdf.parse(s);
        System.out.println(d);
        FileInputStream fis=new FileInputStream("E:/meinv.jpg");
    }

 2.try/catch

 

 

普通形式 

private static void ParseTime(String s) throws ParseException, FileNotFoundException {
        try {
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date d=sdf.parse(s);
            System.out.println(d);
            FileInputStream fis=new FileInputStream("E:/meinv.jpg");
        } catch (ParseException e) {
               System.out.println("格式错误");//e.printStackTrace();这个更好能打印栈异常信息
        } catch (FileNotFoundException e) {
           System.out.println("找不到文件");//e.printStackTrace();
        }
    }

 企业形式

只用父类Exception

private static void ParseTime(String s) throws ParseException, FileNotFoundException {
        try {
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date d=sdf.parse(s);
            System.out.println(d);
            FileInputStream fis=new FileInputStream("E:/meinv.jpg");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

3.第三种方法

    public static void main(String[] args) {
        System.out.println("程序开始");
        try {
            ParseTime("2020-12-12 11:11:11");
            System.out.println("没问题");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("有问题");
        }
        System.out.println("程序结束");
    }
    private static void ParseTime(String s) throws Exception{
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM-dd HH:mm:ss");
        Date d=sdf.parse(s);
        System.out.println(d);
    }

输出结果

程序开始
java.text.ParseException: Unparseable date: "2020-12-12 11:11:11"
    at java.base/java.text.DateFormat.parse(DateFormat.java:399)
    at Exception__handle.thirdMethod.ParseTime(thirdMethod.java:23)
    at Exception__handle.thirdMethod.main(thirdMethod.java:13)
有问题
程序结束

Process finished with exit code 0


只要方便没bug三种方法都可以选择

 4.Demo

输入一个合法价格,如果非法将提示用户重新输入一个合法数据

 public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        while(true){
            try {
                System.out.println("请输入价格");
                String s=sc.nextLine();
                double price= Double.valueOf(s);
                if(price>0){
                    System.out.println("定价"+price);
                    break; 
                }else{
                    System.out.println("请输入一个正数");
                }
            }catch (Exception e){
                System.out.println("输入数据非法");
            }
        }
    }

5.自定义异常

1)自定义编译时异常

先定义一个异常对象,继承Exception

再右键generate后选第二个

定义一个无参构造函数

和public AgeIllegalException(String message) {
        super(message);
    }构造函数

public class AgeIllegalException extends Exception{
    public AgeIllegalException() {
    }
    //右键generate后选第二个
    public AgeIllegalException(String message) {
        super(message);
    }
}

 checkAge方法和main方法如下

 public static void main(String[] args) throws AgeIllegalException {
        checkAge(-34);
    }

    private static void checkAge(int age) throws AgeIllegalException {
        if(age<0 || age>200){
            // throw :在方法内部直接创建一个异常对象,并从此点抛出
            // throws :用在方法申明上的,抛出方法内部的异常

            throw new AgeIllegalException(age + " is illegal");
        }else {
            System.out.println("合法");
        }
    }

输出

Exception in thread "main" Exception__handle.AgeIllegalException: -34 is illegal
    at Exception__handle.ExceptionDemo.checkAge(ExceptionDemo.java:13)
    at Exception__handle.ExceptionDemo.main(ExceptionDemo.java:5)

2)自定义运行时异常

 没那么严格,程序员不太会出错的,如数组越界

继承RuntimeException

调用该方法不用throws就能通过编译

public class AgeIllegalRuntimeException extends RuntimeException{
    public AgeIllegalRuntimeException() {
    }

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

main方法和checkAge2方法

    public static void main(String[] args) {
        //checkAge2(-11);
        //建议最外层用try/catch处理
        try {
            checkAge2(-11);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void checkAge2(int age) {
        if(age<0 || age>200){
            // throw :在方法内部直接创建一个异常对象,并从此点抛出
            // throws :用在方法申明上的,抛出方法内部的异常

            throw new AgeIllegalRuntimeException(age + " is illegal");
        }else {
            System.out.println("合法");
        }
    }

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值