Java基础之:异常

异常

Java 处理错误的机制 不能解决错误 发现错误 提示错误

编译通过之后的代码,在运行过程中,出现错误,Java 会自动根据当前错误创建一个对象,用该对象来表示当前错误,抛出该对象,由对应的代码进行捕获,从而获取到错误对象

public class Test {
    public static void main(String[] args) {
        try {
            int num = 10/0;
        } catch (Exception exception){
            exception.printStackTrace();
        }
    }
}

面向对象编程思想

异常捕获

1、开发者手动进行捕获,灵活进行业务的处理

2、JVM 默认进行捕获,默认输出异常信息

try-catch-finally

Ctrl+Alt+T

finally 无论程序是否有异常,一定会执行的代码

public class Test {
    public static void main(String[] args) {
        System.out.println(test());
    }

    public static int test(){
        try {
            System.out.println("try...");
            return 10;
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            System.out.println("fianlly...");
            return 20;
        }
    }
}

异常类

Java 专门用来描述各种错误的类,一组类,继承关系

最顶端类 Throwable

第二层:Error 和 Exception

Error 表示系统错误,程序无法解决

Exception 表示运行时错误,程序可以解决

Exception 常用的子类

  • ArithmeticException:表示数学运算异常

    int num = 10/0;
    
  • ClassNotFoundException:类未定义异常

    System.out.println(Class.forName("java.lang.StringTest"));
    
  • IllegalArgumentException:参数格式异常

    public class Test {
        public static void main(String[] args) {
            try {
                Class<Test> testClass = Test.class;
                Method test = testClass.getMethod("test", Integer.class);
                test.invoke(new Test(), "123");
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    
        public void test(Integer num){
            System.out.println(num);
        }
    }
    
  • ArrayIndexOutOfBoundsException:数组下标越界异常

    int[] array = new int[3];
    array[3] = 1;
    
  • NullPointerException:空指针异常

    String str = null;
    str.equals("123");
    
  • NoSuchMethodException:方法未定义异常

    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    
    public class Test {
        public static void main(String[] args) throws Exception {
            Class<Test> testClass = Test.class;
            Method test2 = testClass.getMethod("test2", null);
            System.out.println(test2);
        }
    
        public void test(Integer num){
            System.out.println(num);
        }
    }
    
  • NumberFormatException:将其他数据类型转为数值类型的不匹配异常

    String str = "abc";
    Integer integer = Integer.valueOf(str);
    System.out.println(integer);
    

throw 和 throws

throw 和 throws 都是用来抛出异常,开发者手动自动抛出异常

throw 是代码中主动抛出异常对象

throw new ArrayIndexOutOfBoundsException(3);

业务代码开发中,遇到自定义异常的时候,可以通过 throw 进行抛出

throws 表示某个方法可能会抛出异常

在调用该方法的时候就需要明确进行异常的预处理

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值