zoukankan      html  css  js  c++  java
  • Android开发基础(java)14

    Java语言与其他编程语言的最大不同之处在于:java有专门的异常处理机制,实现对各类异常情况进行有效控制。

    一、基本概念:异常和错误的区别与联系:

    (1)、定义形式不同:异常为exception;错误为error

    (2)、处境不同:异常主要是指哪些在程序中可能发生的,经过处理有可能回复正常的非正常事件,通过对事件的处理,实现程序的不中断运行,所以异常的问题一般是非致命的;而错误是指哪些在程序中可能发生的、非常严重且无法回复的非正常事件,这些错误一旦发生,java虚拟机通常会将线程终止或让系统退出运行,错误是程序的致命问题是无法补救的。

    (3)、API的包归属不同:异常归属与exception包,错误归属于error,但是两者都属于Throwable类的范化。程序通常能够处理异常,而无法实现对错误的处理,程序设计者也不需要且不能捕获错误。

    (4)、受检异常(Checked Exception)和非受检异常(Unchecked Exception),非受检异常是指可以发生的合理的异常,这种异常的出现无需要程序员捕获和处理,包括RuntimeException、Error及其子类。非受检异常通常通常反映了程序中的逻辑错误。这种异常可以通过修改代码逻辑,并且不需要程序捕获和处理。非受检异常主要有NullPointerException(空指针异常)和IndexOutOfBoundsException(数组下标越界异常)等。受检异常主要反映了程序运行条件不满足的问题,不能从运行中合理恢复,编译器会强制程序必须处理这种异常。除了RuntimeException和Error及其子类都属于受检异常,这种异常必须用户自行进行处理。受检异常必须显式进行捕获,一般不要自定义受检异常。

    (5)、Exception和RuntimeException类的区别,Exception必须使用try…catch来处理。Error:主要是虚拟机错误,,常常会令虚拟机退出。而RuntimeException可以不使用try…catch处理,直接使用jvm(java virtual machine)(java虚拟机)来处理。自定义的异常通常继承Exception类即可。

    二、异常的产生:主要有throws和throw关键字

    (1)throws关键字主要用来表明方法抛出异常,形式为
    public 返回类型 方法名称(参数列表) throws 异常类{

    ;

    }

    这种方式产生的异常主要是方法抛出异常后,由方法的调用者对异常进行捕获处理。

    (2)throw语句抛出异常对象即可,形式为

    throw 异常对象;

    (3)自定义异常,形式为

    public class 异常类名 extends Exception{

    ……

    }

    三、异常的处理:主要有try、catch和finally关键字,形式为:

    (1)异常的捕获使用:

    try{

    包含有可能抛出异常的代码块

    }catch(异常类型 异常对象)//可以有多个catch语句,对异常进行多次捕获

    [finally{

    无论是否发生异常一定会执行的代码;

    }]

    注意try、catch、finally代码块中的变量是相簿独立的,try块有且只能有一个,catch块可以有一个或多个,不能没有,finally语句块可以有零个或一个。

    (2)二次抛出:

    异常的产生和转交过程称为抛出异常,在catch语句中再次throw 异常对象

    四、常见的异常

    异常 描述 备注
    ArithmeticException 算数异常类  
    NegativeArrayException 数组负下标异常  
    ArrayIndexOutOfBoundsException 数组下标越界异常  
    EOFException 文件已结束异常  
    FileNotFoundException 文件未找到异常  
    IOException 输入输出异常  
    NoSuchMethodException 方法未找到异常  
    NullPointerException 控制针异常类  
    NumberFormatException 字符串转换为数字异常  

    五、代码演示:

    (1)异常的捕获和处理

    1publicclass CatchExceptionDemo{
    2     publicstatic void function(){
    3         Object[] obj =new String[4];
    4         for(int i =0; i < obj.length; i++){
    5             try{
    6                 if(i %2 == 1){
    7                     obj[i] =new Integer(i);//程序在这里会抛出数组变量类型不匹配的异常
    8                 }else{
    9                     obj[i] =new Integer(i) + " ";
    10                 }
    11                 System.out.print(" " + obj[i] +" ");
    12             }catch(ArrayStoreException exp){//这里对异常进行选择性的捕获
    13                 System.out.println("出现ArrayStoreException异常,数组内的元素类型与数组类型不匹配!");
    14             }finally{
    15                 System.out.println("这是程序无论如何都会执行的代码部分!");//这里的代码是无论如何都会执行的部分
    16                 System.out.println();
    17             }
    18         }
    19         System.out.println("function方法执行结束!");
    20     }
    21  
    22     publicstatic void main(String[] args){
    23         function();
    24         System.out.println("程序执行完成!");
    25     }
    26 }


    编译执行的结果:
     0  
    这是程序无论如何都会执行的代码部分!

    出现ArrayStoreException异常,数组内的元素类型与数组类型不匹配!
    这是程序无论如何都会执行的代码部分!

    2  
    这是程序无论如何都会执行的代码部分!

    出现ArrayStoreException异常,数组内的元素类型与数组类型不匹配!
    这是程序无论如何都会执行的代码部分!

    function方法执行结束!
    程序执行完成!

    请按 ENTER 或其它命令继续

    (2)自定义异常方法:
    1publicclass ThrowsExceptionDemo{
    2     publicvoid catchThrowException(String str)throws NullPointerException, ArrayIndexOutOfBoundsException, Arithme
      ticException{
    3         System.out.print(str +"");
    4         switch(str){
    5             case"除零异常":
    6                 {
    7                     int a =0;
    8                     int b =6 / a;
    9                 }
    10                 break;
    11             case"空指针异常":
    12                 {
    13                     String[] s =new String[5];
    14                     s[0].toCharArray();
    15                 }
    16                 break;
    17             case"数组下标越界异常":
    18                 {
    19                     String[] s =new String[5];
    20                    s[5] ="c";
    21                 }
    22                 break;
    23             default:
    24                 System.out.println("方法运行一切正常!");
    25         }
    26     }    
    27  
    28     publicstatic void main(String[] args){
    29         ThrowsExceptionDemo te02 =new ThrowsExceptionDemo();
    30         try{
    31             te02.catchThrowException("无异常");
    32         }catch(Exception exp){
    33             System.out.println(": " + exp.getMessage());
    34         }
    35          
    36         try{
    37             te02.catchThrowException("除零异常");
    38         }catch(Exception exp){
    39             System.out.println(": " + exp.toString());
    40         }
    41          
    42         try{
    43             te02.catchThrowException("空指针异常");
    44         }catch(Exception exp){
    45             System.out.println(": " + exp);
    46         }
    47          
    48         try{
    49             te02.catchThrowException("数组下标越界异常");
    50         }catch(Exception exp){
    51             System.out.println("异常: " + exp);
    52         }
    53     }
    54 }

    运行结果:
    无异常方法运行一切正常!
    除零异常: java.lang.ArithmeticException: / by zero
    空指针异常: java.lang.NullPointerException
    数组下标越界异常异常: java.lang.ArrayIndexOutOfBoundsException: 5

    请按 ENTER 或其它命令继续
    上课代码:

    //public class ExceptionDemo extends Exception{
    public class ExceptionDemo extends Throwable{
        public static void main(String[] args) {
            int[] a = new int[4];
            for(int i = 0; i < a.length; i++){
                a[i] = i;
            }
            for(int i : a){
               try{
                   if(i % 2 == 0){
                        throw new ExceptionDemo();
                    }else{
                        System.out.println(i + "");
                    }
               }catch(ExceptionDemo  | IndexOutOfBoundsException exp){
                   System.out.println("数组下标越界");
               }finally{
                   System.out.println("无论如何,本行代码均会执行");
               }           
            }
            }
        }   //public class ExceptionDemo extends Exception{
    public class ExceptionDemo extends Throwable{
        public static void main(String[] args) {
            int[] a = new int[4];
            for(int i = 0; i < a.length; i++){
                a[i] = i;
            }
            for(int i : a){
               try{
                   if(i % 2 == 0){
                        throw new ExceptionDemo();
                    }else{
                        System.out.println(i + "");
                    }
               }catch(ExceptionDemo  | IndexOutOfBoundsException exp){
                   System.out.println("数组下标越界");
               }finally{
                   System.out.println("无论如何,本行代码均会执行");
               }           
            }
            }
        }   //public class ExceptionDemo extends Exception{
    public class ExceptionDemo extends Throwable{
        public static void main(String[] args) {
            int[] a = new int[4];
            for(int i = 0; i < a.length; i++){
                a[i] = i;
            }
            for(int i : a){
               try{
                   if(i % 2 == 0){
                        throw new ExceptionDemo();
                    }else{
                        System.out.println(i + "");
                    }
               }catch(ExceptionDemo  | IndexOutOfBoundsException exp){
                   System.out.println("数组下标越界");
               }finally{
                   System.out.println("无论如何,本行代码均会执行");
               }           
            }
            }
        }   
     
     
    public class Chapter092 {

        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            //String str = null;
            try{
                functionException("数组越界");
               
            }catch(IndexOutOfBoundsException exp){
                System.out.println("数组下标异常捕获");
            }
            try{
                functionException("除数为零");
            }catch(ArithmeticException exp){
                System.out.println("除数为零异常捕获");
            }
            functionException("");
        }
        public static void functionException(String string) throws IndexOutOfBoundsException, ArithmeticException{
            //String str = null;
            switch(string){
                case "数组越界":
                {
                    int[] a = new int[4];
                    System.out.println("数组下标异常发生");
                    a[4] = 5;
                }
                break;
                case "除数为零":
                {
                    int a = 0;
                    int b = 6;
                    System.out.println("除数为零异常发生");
                    int c = b / a;
                }
                break;
                default:
                    System.out.println("程序没有异常!");
            }
        }
       
    }

  • 相关阅读:
    Javascript操作cookie
    上传文件-layui+ashx
    Mysql对查询结果添加序列号
    Maven详解
    解决 go iris ReadJSON 无法获取 json,并提示 unexpected end of JSON input 的错误
    Archlinux 最新安装方法 (2020.07.01-x86_64)之虚拟机 BIOS 安装
    Oracle APEX 发送邮件
    包管理Go module的使用
    Golang 解决 Iris 被墙的依赖包
    解决 Nginx 代理Apex慢的问题
  • 原文地址:https://www.cnblogs.com/guochaoxxl/p/6823166.html
Copyright © 2011-2022 走看看