“抛弃”异常

作者: 白晓明 | 来源:发表于2019-04-13 23:39 被阅读0次

    什么是异常?

    • 异常指在程序运行过程中发生的错误或者不正常的情况,从而导致程序中断的指令流。
    • 异常时针对方法而言的,抛出、声明抛出、捕获和处理异常都是在方法中进行的。

    Java提供的异常处理类

    异常类名称 异常类含义
    ArithmeticException 算术异常类
    ArratIndexOutOfBoundsException 数组下标越界异常类
    ArrayStoreException 数组元素类型错误异常类
    ClassCastException 类型强制转换异常类
    ClassNotFoundException 未找到对应的类抛出的异常
    EOFEException 文件已结束异常类
    FileNotFoundException 文件未找到异常类
    IllegalAccessException 访问某类被拒绝时抛出的异常
    InstantiationException 创建一个抽象类或抽象接口类的实例时抛出的异常
    IOEException 输入/输出抛出的异常
    NegativeArraySizeException 建立元素个数为负数的异常类
    NullPointerException 空指针异常
    NumberFormatException 字符串转换数组异常
    NoSuchFieldException 字段未找到异常
    NoSuchMethodException 方法未找到异常
    SQLException 操作数据库异常
    StringIndexOutOfBoundsException 字符串索引超出范围异常

    Java 异常处理方式

    我们在编写Java程序时,需要考虑程序的健壮性。在编写代码时,我们要考虑代码在各种环境下是否会出现异常情况,出现异常后我们应该怎么去处理。

    //我们可以通过try{} catch{} 来捕获异常并处理异常
    try {
    
    } catch(Exception e) {
        //可以通过throw将异常抛出
        throw new Exception();
    } catch(Exception e) {
        //也可以通过自定义的方式处理异常,比如将异常信息保存到数据库
        //通过使用getMassage()方法获取异常的详细描述字符串
    } catch(Exception e) {
        //也可以将异常信息保存到Log日志文件中
    }
    
    //我们也可以使用throws来处理异常
    //在方法上声明异常处理
    void methodName(int a) throws ArithmeticException {
          //编写代码
    } 
    

    自定义异常

    Throwable 类是 java.lang 包中一个专门用于处理异常的类。其有两个子类Error(用来处理运行环境方面的异常)和Exception(用于处理Java程序运行时常常遇到的各种异常以及非运行时异常)

    /*
     * 自定义异常使其继承自Throwable类
     */
    public class LoginThrowable extends Throwable{
    
        private static final long serialVersionUID = 1L;
    
        public LoginThrowable(String str) {
            super(str);
        }
        
        public LoginThrowable(String str, Throwable cause){
            super(str, cause);
        }
        
        public LoginThrowable(Throwable cause) {
            super(cause);
        }
    }
    
    public class TestCustomThrowable {
    
        /*
         * 调用自定义异常类,并给出异常信息
         */
        public static void LoginException() throws LoginThrowable {
            throw new LoginThrowable("登录账户或密码错误!");
        }
        
        /*
         * 测试自定义异常类
         */
        public static void main(String[] args){
            try {
                TestCustomThrowable.LoginException();
            } catch (LoginThrowable e) {
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
        }
    }
    
    返回结果如下:

    登录账户或密码错误!
    top.baixiaoyuan.throwable.LoginThrowable: 登录账户或密码错误!
    at top.baixiaoyuan.throwable.TestCustomThrowable.LoginException(TestCustomThrowable.java:7)
    at top.baixiaoyuan.throwable.TestCustomThrowable.main(TestCustomThrowable.java:12)

    在项目开发中,对于异常我们一般都是根据实际情况以编号信息相结合的方式返回,提示用户操作出错的原因。

    /*
     * 自定义一个接受异常信息的类
     */
    public class CustomTrowable extends Exception {
    
        private int code;
        private String msg;
        
        public CustomTrowable(int code, String msg) {
            super();
            this.code = code;
            this.msg = msg;
        }
        
        @Override
        public String toString() {
            return "异常代码:" + getCode() + ",异常信息:" + getMsg();
        }
        
        public int getCode() {
            return code;
        }
        public void setCode(int code) {
            this.code = code;
        }
        public String getMsg() {
            return msg;
        }
        public void setMsg(String msg) {
            this.msg = msg;
        }
    }
    
    //测试抛出异常
    public static void main(String[] args) throws CustomTrowable {
        File file = new File("D:\tmp\test.doc");
        try {
            InputStream in = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            throw new CustomTrowable(200, e.getMessage());
        }
    }
    
    运行结果

    Exception in thread "main" 异常代码:200,异常信息:D: mp est.doc (文件名、目录名或卷标语法不正确。) at top.baixiaoyuan.throwable.TestCustomThrowable.main(TestCustomThrowable.java:29)

    相关文章

      网友评论

        本文标题:“抛弃”异常

        本文链接:https://www.haomeiwen.com/subject/gahkwqtx.html