【Java异常概述】

Error

(1)OutOfMemoryError 内存溢出错误
(2)StackOverflowError 栈内存溢出错误
(3)NoClassDefFoundError 找不到class定义的错误(删类跑路)
错误一般发生在严重故障时,它们在Java程序处理的范畴之外。

Exception

两类:一、检查性异常 二、运行时异常(也叫非检查性异常)

  1. 所有异常都必须是 Throwable 的子类。
  2. 如果希望写一个检查性异常类,则需要继承 Exception 类。
  3. 如果你想写一个运行时异常类,那么需要继承 RuntimeException 类。
    Exception
    抛出异常顺序会根据入栈出栈,会抛给调用者
    e不打印会生吞异常,不做任何事情
    大范围用超类,小范围用子类抛异常
    抛异常用throw 捕获用catch捕获 e进行打印堆栈信息,真正的异常还是要找的

异常链

JDk1.4以前需要自己写代码保存异常信息
我们现在用的大多数都是1.8所以就用inItCause就行了

public class Test {
    public static void fun(){
        String username;
        String Password ;
            Scanner scanner=new Scanner(System.in);
            System.out.println("请输入用户名");
            username= scanner.next();
            if (!"admin".equals(username) ){
                System.out.println("输入不合法");
                throw new UserException(504,"用户名错误");
            }
            Scanner scanner1=new Scanner(System.in);
            System.out.println("请输入密码");
            Password= scanner1.next();
            if (!"123".equals(Password)){
                throw  new PassWordException();
            }
        }
    public static void fun2(){
        try {
            fun();
        }catch (UserException e){
          throw new PassWordException(e);
        }
        catch (PassWordException e){
            System.out.println("密码错误");
        }
    }
    public static void main(String[] args) {
        while (true){
            fun2();
        }
    }
}
public class PassWordException extends RuntimeException{
    public PassWordException() {
    }
    public PassWordException(Throwable cause) {
        super(cause);
    }
}
public class UserException extends RuntimeException{
    public UserException() {
    }
    public UserException(int code,String message) {
        super(message);
        this.code = code;
    }
    private int code;
    public int getCode() {
        return code;
    }
    public void setCode(int code) {
        this.code = code;
    }
}

正文

throws在方法签名处抛(检查类异常)
throw在方法内部抛(运行时异常)
(1)常见的检查性异常

  • IOException IO异常,在对流操作时有可能会出现的异常
  • SQLException SQL异常
  • ClassNotFoundException 找不到某个类时,会抛出该异常
  • InterruptedException 当阻塞方法收到中断请求的时候就会抛出中断异常

(2)常见的运行时异常

  • NullPointerException 空指针引用异常,视图调用空对象的方法或者属性时,抛出该异常
  • ArithmeticException 算术运算异常
  • ClassCastException 类型转换异常,当试图将对象强制转换为不是实例的子类时,抛出该异常
  • IndexOutOfBoundsException 下标越界异常
  • NumberFormatException 数字格式异常
    方法返回值有两种类型:值类型与对象引用。对于对象引用,要特别小心,如果在finally代码块中对返回的对象成员属性进行了修改,即使不在finally块中显式调用return语句,这个修改也会作用于返回值上。

面试题
面试题一

public static void main(String[] args){
    int result = test1();
    System.out.println(result);
}

public static int test1(){
    int i = 1;
    try{
        i++;
        System.out.println("try block, i = "+i);
    }catch(Exception e){
        i--;
        System.out.println("catch block i = "+i);
    }finally{
        i = 10;
        System.out.println("finally block i = "+i);
    }
    return i;
}

面试题二

public static int test2(){
    int i = 1;
    try{
        i++;
        throw new Exception();
    }catch(Exception e){
        i--;
        System.out.println("catch block i = "+i);
    }finally{
        i = 10;
        System.out.println("finally block i = "+i);
    }
    return i;
}

面试题三

public static void main(String[] args){
    int result = test3();
    System.out.println(result);
}

public static int test3(){
    //try 语句块中有 return 语句时的整体执行顺序
    int i = 1;
    try{
        i++;
        System.out.println("try block, i = "+i);
        return i;
    }catch(Exception e){
        i ++;
        System.out.println("catch block i = "+i);
        return i;
    }finally{
        i = 10;
        System.out.println("finally block i = "+i);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

长安归故里♬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值