zoukankan      html  css  js  c++  java
  • Java(十一)——异常处理

    异常

    一、捕获异常语法

    • try...catch...finally...finally表示无论是否有异常发生,都会执行其中的语句
    • 异常catch的顺序必须是子类在上面
    try {
        String s = processFile(“C:\test.txt”);
        // ok:
    } catch (FileNotFoundException e) {
        // file not found:
    } catch (SecurityException e) {
        // no read permission:
    } catch (IOException e) {
        // io error:
    } catch (Exception e) {
        // other error:
    }finally{
        System.out.println("testEx2, finally");
    }
    

    异常继承树:

    image-20200107193021636

    二、主动抛出异常

    注意:

    主动抛出异常时必须 try...catch...捕获并处理掉

    1、函数内部抛出异常

    // throw new 异常类
    public static void main(String[] args) {
    	String str = "aaaa";
            try {
                if(str.equals("aaaa")){
                    throw new Exception("抛出异常");
    
                }
            }catch (Exception e){
                System.out.print(e);
            }
    
    }
    

    2、整个方法抛出异常

    在函数体之前函数名之后:throws Exception

    // 抛出可能存在的所有异常
    public static void main(String[] args) throws Exception {
    	String str = "aaaa";
            try {
                if(str.equals("aaaa")){
                    throw new Exception("抛出异常");
    
                }
            }catch (Exception e){
                System.out.print(e);
            }
    
    }
    

    三、自定义异常

    • 自定义异常时,应当继承Exception
    • 自定义异常时,应该提供多种构造方法
    public class CustomException extends Exception {
    
        //无参构造方法
        public CustomException(){
            super();
        }
    
        //有参的构造方法
        public CustomException(String message){
            super(message);
        }
    
        // 用指定的详细信息和原因构造一个新的异常
        public CustomException(String message, Throwable cause){
            super(message,cause);
        }
    
        //用指定原因构造一个新的异常
         public CustomException(Throwable cause) {
             super(cause);
         }
    
    }
    

    四、断言(很少用)

    使用关键字assert。如果assert后面的条件不满足就会抛出AssertionError

    • 断言只能在开发和测试阶段使用
    • JVM默认关闭断言指令,即遇到assert语句就自动忽略了,不执行
    • 要执行assert语句,必须给Java虚拟机传递-enableassertions(可简写为-ea)参数启用断言。所以,必须在命令行下运行才能执行assert语句
    public static void main(String[] args) {
        double x = Math.abs(-123.45);
        assert x >= 0;
        System.out.println(x);
    }
    

    执行:

    终端中
    $ java -ea Main.java
    

    五、日志

    1、使用日志

    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    public class Hello {
        public static void main(String[] args) {
            Logger logger = Logger.getGlobal();
            logger.info("start process...");
            logger.warning("memory is running out...");
            logger.fine("ignored.");
            logger.severe("process will be terminated...");
        }
    }
    

    2、日志级别

    // 日志级别从高到低依次为:
    SEVERE
    WARNING
    INFO
    CONFIG
    FINE       //  默认级别是INFO,INFO级别以下的日志,不会被打印出来
    FINER
    FINEST
    

    3、日志框架(常用)

    SLF4J和Logback

    https://www.liaoxuefeng.com/wiki/1252599548343744/1264739155914176

  • 相关阅读:
    使用Hugo框架搭建博客的过程
    使用Hugo框架搭建博客的过程
    使用Hugo框架搭建博客的过程
    Windows软件包管理工具:Scoop
    Centos8 安装ifconfig(net-tools.x86_64)
    Centos8 重启网卡方法
    使用Visual Studio 2019--调试汇编32位代码的详细步骤
    linux 三剑客之awk总结
    linux 三剑客之sed常用总结
    mysql数据库的笔记
  • 原文地址:https://www.cnblogs.com/linagcheng/p/12166810.html
Copyright © 2011-2022 走看看