• 面向对象(异常)


    Exception:程序在运行时出现不正常的情况(注意,运行时出现的,不是编译时出现)

    java对问题的描述:
    1.严重的问题——>Error
    2.不严重的问题——>Exception
    --------------------------------------------
    来看这个例子
    用我的计算机,定义的这个字节数组长度是1340Mb,就会报出异常,内存溢出异常
    这说明,我的计算机上线的java 虚拟机定义的内存就这么大
    public static void main(String[] args) {
            byte[] b = new byte[1024*1024*1340];
        }
    -----------------------------------------------
    错误和异常都有父类:Throwable,
    Throwable
            |——Error
            |——Exception
    -------------------------------------------------
    异常的处理:
    try{
        可能出异常的代码
    }catch(异常类 变量){
        处理异常的代码(一般都是打印异常)
    }finally{
        处理完异常后再执行的语句,且一定会执行
    }
    --------------------------------------------------
    异常的运行机制是这样的
    如果,在main函数中出现异常,由于main无法处理这些异常,而main的底部是虚拟机,
    于是虚拟机就处理这些异常,报出异常,但这也意味着,程序崩溃了
    而有了try/catch之后,try/catch就能处理这些异常,
    于是try监测的异常,就通过异常参数,传给了catch处理,这样能保证程序继续跑下去
    ------------------------------------------------------
    通常我们都是在catch里面打印异常,那有没有别的方式呢
    举个栗子~ Syso(e.getMessage());打印异常语句,具体有哪些方法,自己去看文档
    e.printStackTrace();这个更详细:其实jvm默认的异常处理机制,就是在调用printStackTrace方法。
    --------------------------------------------------------
    抛出异常 throws Exception
    在功能上通过throws的关键字声明了该功能有可能会出现问题。
    class Demo
    {
        int div(int a,int b)throws Exception//在功能上通过throws的关键字声明了该功能有可能会出现问题。
        {
            return a/b;
        }
    }
     
    class  ExceptionDemo1
    {
        public static void main(String[] args)throws Exception
        {
            Demo d = new Demo();
            int x = d.div(4,0);
            System.out.println("over");
    }
    -------------------------------
    看上面代码,int div方法定义了一个抛异常的东西,main函数也定义了,那么,异常就会这样
    异常div(4,0)——>抛给 main——>抛给虚拟机JVM
    对可能出异常的部分,不能都抛给虚拟机啊,你得对可能出异常的部分捕捉一下啊
    就用try/catch
    public static void main(String[] args) //throws Exception
        {
            Demo d = new Demo();
            try
            {
                int x = d.div(4,0);
                System.out.println("x="+x);
            }
            catch (Exception e)
            {
                System.out.println(e.toString());
            }
    -------------------------------------------------------
    对多异常的处理。
     
    1,声明异常时,建议声明更为具体的异常。这样处理的可以更具体。
    2,对方声明几个异常,就对应有几个catch块。不要定义多余的catch块。
        如果多个catch块中的异常出现继承关系,父类异常catch块放在最下面。
     
    建立在进行catch处理时,catch中一定要定义具体处理方式。
    不要简单定义一句 e.printStackTrace(),
    也不要简单的就书写一条输出语句。一般的都是将异常放到一个文件里去,异常日志
    ------------------------------------------
    class Demo
    {
        int div(int a,int b)throws ArithmeticException,ArrayIndexOutOfBoundsException
        {
     
            int[] arr = new int[a];
     
            System.out.println(arr[4]);
     
            return a/b;
        }
    }
     
     
    class  ExceptionDemo2
    {
        public static void main(String[] args) //throws Exception
        {
            Demo d = new Demo();
            try
            {
                int x = d.div(5,0);
                System.out.println("x="+x);
            }
      catch (ArithmeticException e)
            {
                System.out.println(e.toString());
                System.out.println("被零除了!!");
     
            }
      catch (ArrayIndexOutOfBoundsException e)
            {
                System.out.println(e.toString());
                System.out.println("角标越界啦!!");
            }
     catch(Exception e)
            {
                System.out.println("hahah:"+e.toString());
            }
    如果最底下这个catch放在第一的位置,那上面两个就变成废话了,因为这句catch能处理所有异常
    -------------------------------------------------------------
    自定义异常
    对自己的项目里的特有的问题进行捕获
  • 相关阅读:
    chrome 浏览器安装 postman
    react + antd Form表单校验
    Java 获取日期间的日期 & 根据日期获取星期
    PostgreSQL 按照日期范围查询
    momentjs 学习
    npm 常用命令
    Web中的安全性问题
    (转)Java 详解 JVM 工作原理和流程
    HashMap的工作原理
    对于org.apache.commons.dbcp.BasicDataSource的配置认知
  • 原文地址:https://www.cnblogs.com/aisi-liu/p/4227627.html
Copyright © 2020-2023  润新知