JAVA基础(异常的捕获与处理)

一,异常:是导致程序中断执行的指令流,一旦产生异常之后,产生异常及以后的语句都不再执行,自动结束程序的执行。
二,处理产生的异常

  • try…catch…finally
  • try…catch
  • try…finally
  • 捕获异常:try{可能产生异常的代码},捕获产生异常语句后直接跳转catch,异常后的语句不再执行
  • 处理异常:catch(异常类型 异常对象){处理异常},catch可以有多个
  • finally{不管是否产生异常都会执行}
package 异常;
public class ExceptionDemo {
    public static void main(String[] args) {
        try {
            System.out.println("开始");
            int result = 10/0;
            System.out.println(result);
        }catch (Exception e)
        {
           // e.printStackTrace();
            System.out.println("发生异常:除数不能为零");
        }finally
        {
            System.out.println("结束");
        }

    }
}


几点说明:

  • Exception类是所有异常类的父类,因此在不知道会出现什么异常的时候,catch中统一使用Exception(向上转型)处理,
  • 多个catch捕获异常时,捕获范围大的异常放在捕获范围小的后面,否则程序出现错误
  • 异常和错误的区别是:异常能被程序本身可以处理,错误是无法处理
  • Exception(异常)分两大类:运行时异常和非运行时异常(编译异常)。程序中应当尽可能去处理这些异常。
  • 运行时异常:都是RuntimeException类及其子类异常,如NullPointerException(空指针异常)、IndexOutOfBoundsException(下标越界异常)等,这些异常是不检查异常,程序中可以选择捕获处理,也可以不处理。这些异常一般是由程序逻辑错误引起的,程序应该从逻辑角度尽可能避免这类异常的发生。运行时异常的特点是Java编译器不会检查它,也就是说,当程序中可能出现这类异常,即使没有用try-catch语句捕获它,也没有用throws子句声明抛出它,也会编译通过。
  • 非运行时异常 (编译异常):是RuntimeException以外的异常,类型上都属于Exception类及其子类。从程序语法角度讲是必须进行处理的异常,如果不处理,程序就不能编译通过。如IOException、SQLException等以及用户自定义的Exception异常,一般情况下不自定义检查异常。

附:程序异常处理流程图:
在这里插入图片描述
三,抛出异常:throws主要用于方法的声明上,表示方法中产生的异常交给调用处处理,但不一定会产生异常,表示一种可能性,不管是否产生异常都要用try…catch。throw,表示抛出一个异常对象,表示一种动作。

  • throws
package 异常;
public class ExceptionDemo {
    public static void main(String[] args) {

        try {
            System.out.println("开始");
            int x = 10;
            int y = 0;
            div(x, y);
        } catch (Exception e) {
            System.out.println("发生异常:除数不能为零");
        } finally
        {
            System.out.println("结束");
        }
    }
    public static void div(int x, int y) throws Exception {
        System.out.println(x / y);
    }
}

在这里插入图片描述

  • throw
package 异常;
public class ExceptionDemo {
    public static void main(String[] args) {
        String s= null;
        if(s==null)
        {//抛出空指针异常
            throw new NullPointerException();
        }
        else
        {
            System.out.println(s);
        }
    }

}

  • throw and throws---->异常处理的标准化
package 异常;
public class ExceptionDemo {
    public static void main(String[] args) {
        try {
            int x = 10;
            int y = 0;
            System.out.println(div(x, y));
        } catch (Exception e) {
            System.out.println("发生异常:除数不能为零");
        }
    }
    public static int div(int x, int y) throws Exception {
        int result = 0;
        try {
            System.out.println("开始");
             result = x/y;
        }catch (Exception e)
        {
            throw e;//将产生的异常抛出,再由throws抛到调用处处理
        }finally
        {
            System.out.println("结束");
        }
        return result;
    }
}

四,RuntimeException类:运行时异常类(NullPointerException,ArithmeticException,ClassCastException)。程序在编译时,不会强制性要求用户处理,如果产生了异常,将交给JVM默认处理。RuntimeException的子类可以可以根据用户需求选择性处理,Exception类型的异常必须处理。

五,自定义异常:

package 异常;
class selfException extends Exception
{
    public selfException(String ex)
    {
        super(ex);
    }
}
public class ExceptionDemo {
    public static void main(String[] args) {
        try {
            int num = 10;
            if(num>=10)
            {
                throw new selfException("自定义异常");
            }
        }catch (Exception e)
        {
            e.printStackTrace();
        }
        }

}

在这里插入图片描述

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值