zoukankan      html  css  js  c++  java
  • 第33节:Java面向对象中的异常

    标题图

    Java中的异常和错误

    Java中的异常机制,更好地提升程序的健壮性

    throwable为顶级,ErrorException

    Error:虚拟机错误,内存溢出,线程死锁

    ExceptionRuntimeException为空指针异常,数组下标越界异常,算数异常,类型转换异常等,IO异常(IOException),SQL异常(SQLException)。

    异常处理,在Java中异常处理机制为:抛出异常捕捉异常

    异常的描述:

    class ExceptionDemo{
     public static void main(String[] args){
      int[] arr = new int[3];
      System.out.println(arr[0]);
      // 结果 为 0
      System.out.println(arr[3]);
      // 结果 为 图1
     }
    }

    图1

    System.out.println(arr[3]);编译没问题,语法没有问题,编译完内存中没数组,运行的时候才在堆内存中开辟数组空间。arr[3]没有这个下标,所以在运行时找不到结果。

    图1,表示数组下标越界异常,System.out.println(arr[3]);运行时发生了异常为ArrayIndexOutOfBoundException,导致了程序无法运行,程序终结,不在执行。

    错误的描述

    class ExceptionDemo{
     public static void main(String[] args){
      int[] arr = new int[1024*1024*1024];
      System.out.println(arr[0]);
      // 结果 为 0
      System.out.println(arr[3]);
      // 结果 为 图1
     }
    }

    图2

    图2,表示运行时发生的错误,堆内存溢出。

    异常和错误的区别

    对于异常是由解决方案的,Java中提供了对应的处理机制,而错误没有,是没有办法去针对性的解决,唯一方法就是错误出现,修改代码。

    异常的过程

    在异常情况,运行时发生的问题,是数组下标越界异常,在异常抛出的问题为名称,内容,发生的位置等,多种信息进行了封装到对象中。

    ArrayIndexOutOfBoundsException

    public class ArrayIndexOutOfBoundsException extends IndexOutOfBoundsException

    构造方法

    ArrayIndexOutOfBoundsException()
    ArrayIndexOutOfBoundsException(int index)
    ArrayIndexOutOfBoundsException(String s);

    ArrayIndexOutOfBoundsException可以new对象,有构造方法,就可以new对象。创建对象,如果遇到问题就抛出,new ArrayIndexOutOfBoundsException(index)

    如何抛出呢?利用关键字throw,出现异常,在Java虚拟机,jvm中需要把问题抛出,给调用者main,主函数收到抛出的异常对象,但主函数没有办法处理,继续抛出调用者jvmjvm收到异常问题后,将异常信息显示在屏幕上。

    异常概述(意外、例外)

    什么是异常呢?常见:

    try-catch-finally
    // try-catch-finally
    public void method(){
        try {
              // 代码段
              // 产生异常的代码段
            }catch (异常类型 ex) {
              // 对异常进行处理的代码段
            }finally{
              // 代码段
            }
    }
    throw
    throws

    throws 声明时要进行抛出的异常
    throw 要手动将产生的异常抛出

    public void method() throws Exception1,Exception2,...,ExceptionN {
        // 产生异常代码
    }
    // throw new IOException();
    // 自己抛出的问题自己进行异常解决。
    // 在抛出异常处,通过throws关键字标明异常类型
    public void method() throws 异常类型{
      // 代码 抛出
        throw new 异常类型();
    } 
    自定义异常
    异常链

    异常处理分类为:

    1. 抛出异常
    2. 捕捉异常

    简单案例

    public class Test{
     public static void main(String[] args){
      try{
        String msg = redText(“C:\a.txt”);
      }catch(PathNotExistException e){
        // 进行处理
      }
     }
    }

    Throwable异常的顶级父类

    异常Exception处理方式有两种,一为捕获,二为继续抛出编译时的异常

    RuntimeException运行时异常,只有在运行的时候才会出现,可以处理,也可以不处理。

    自定义异常,可以自己定义异常,自己定义一个类,如果这个类继承某个异常类,继承的是Exception或其他异常,即定义了一个编译时异常,如果继承的是运行时异常RuntimeException或是它的子类,就定义了一个运行时异常

    Throwable类是Java中所有错误或异常的超类,只有当对象是这个类的实例时,能通过虚拟机或是Javathrow语句抛出。

    Exception分为两大类

    1. 非检查异常(Unchecked Exception):编译器不要求强制处理异常
    2. 检查异常(Checked Exception):编译器要求必须处理的异常,如IO异常等

    捕获异常

    1. try:执行可能产生异常的代码
    2. catch:捕获异常
    3. finally:无论是否发生异常代码总能执行

    声明异常,抛出异常

    1. throws:声明可能要抛出的异常
    2. throw:手动抛出异常

    如果某方法出现了异常,却是没有能力的处理,可以在方法处用throws来声明抛出异常,谁调用这个方法,谁就去处理这个异常。

    public void method() throws Exception1,Exception2,...,ExceptionN {
        // 异常的代码
    }

    Java中的异常处理情况

    JAVA 异常

    try...catch...finally结构的使用方法

    class Test{
    public static void main(String args[]){
    
     try{ 
      int i = 1 / 0;
      }
      catch(Exception e){
       e.printStackTrace();
      }
      finally{
       System.out.println("finally");
      }
      System.out.println(5);
     }
    }
    
    class Test{
    public static void main(String args[]){
     try{
      Thread.sleep(1000);
     }
     catch(Exception e){
      e.printStackTrace();
     }
    }
    }

    throwthrows的作用区别:

    class Person{
    private int age;
    
    public void setAge(int age) throws Exception{
     if(age<0){
      RuntimeException e = new RuntimeException("年龄不能小于0");
      throw e;
     }
     this.age = age;
     }
    }
    
    class Test{
    public static void main(String args[]){
     Person person = new Person();
     try{
      person.setAge(-1);
     }
     catch(Exception e){
      System.out.println(e);
     }
    }
    }

    Error和Exception的区别

    • ErrorThrowable的子类,用于标记严重错误
    • ExceptionThrowable的子类,指示合理的程序想去catch的条件,非严重错误

    try/catch的执行过程

    如果出现异常,系统则会抛出一个异常,
    进行捕捉(catch操作),或在最后(finally)来进行处理。

    throw和throws的区别

    throws 出现在方法声明上,throw出现在方法体内。

    异常分类

    异常分类:可查异常运行时异常错误

    异常链

    异常链为我们捕获一个异常后再抛出另一个异常

    one -> two -> three

    结语

    • 下面我将继续对JavaAndroid中的其他知识 深入讲解 ,有兴趣可以继续关注
    • 小礼物走一走 or 点赞
  • 相关阅读:
    PAT A1094 The Largest Generation (25 分)——树的bfs遍历
    PAT A1055 The World's Richest (25 分)——排序
    PAT A1052 Linked List Sorting (25 分)——链表,排序
    PAT A1076 Forwards on Weibo (30 分)——图的bfs
    辅导员
    辅导员面试
    C程序设计
    Excel VBA 基本概念
    Excel函数
    导入excel表的数据到数据库ssh
  • 原文地址:https://www.cnblogs.com/dashucoding/p/11932486.html
Copyright © 2011-2022 走看看