• 08.异常处理机制


    在Java中我们可以将错误分为两种

    一类是Error,在Java中用Error类表示。意味着发生了JVM检测到到的严重错误,导致JVM无法继续运行。这种错误程序是不能处理的,因为程序无法捕捉到它,也就难以采用相应的处理措施。

    另一类在Java中称为异常类,表示是可以恢复的,程序能够捕捉到,因此在程序中可以采取适当的措施进行处理。

    一.Java中的异常处理机制

    在Java语言中,我们将程序运行时有可能产生的错误称为异常,有时也称为例外。

    Java中引入了异常和异常类,异常是异常类的对象。

    Java中的异常处理机制就是:Java程序在运行中,当发生一个可以辨别的异常时,都有这个异常的异常类与之对应,并产生该异常类的一个对象,系统就会有相应的机制来处理异常。

    采用异常处理机制,使得异常处理代码和程序中的其他部分隔开,增加程序的清晰性与可读性。

    二.Java异常处理类

    Java异常类由系统库中Exception类派生的,而Exception类由throwable类派生

    Java.lang.Object
    	java.lang.Throwable
    		java.lang.Error//错误--操作系统保留
    		java.lang.Exception//异常--应用程序使用
    

    Exception类

    • 层次图

    异常类型 说明
    Exception 异常层次结构的根类
    RuntimeException 运行时异常,多数 java.lang 异常的根类
    ArithmeticException 算术谱误异常,如以零做除数
    ArraylndexOutOfBoundException 数组大小小于或大于实际的数组大小
    NullPointerException 尝试访问 null 对象成员,空指针异常
    ClassNotFoundException 不能加载所需的类
    NumberF ormatException 数字转化格式异常,比如字符串到 float 型数字的转换无效
    IOException I/O 异常的根类
    F ileN otF oundException 找不到文件
    EOFException 文件结束
    InterruptedException 线程中断
    IllegalArgumentException 方法接收到非法参数
    ClassCastException 类型转换异常
    SQLException 操作数据库异常
    • 构造方法

    public Exception();
    public Exception(String s)
    
    • 从父类Throwable继承的常用方法

    public String toString()//返回描述Exception类信息的字符串
    public void printStackTrace()//
    

    三.异常的处理

    Java的异常处理有以下几种方式:

    对运行时异常可以不做处理,由Java系统检测

    用try - catch - finally 捕获异常

    用throws句子抛出

    用户自定义异常

    捕获异常

    try{
    	//可能产生异常的代码片段
    }catch(异常类型 异常变量){
    	//异常处理
    }[finally{
    	//不管是否发生异常总要执行的代码
    }]
    
    import java.io.*;
    public class ExcepTest{
     
       public static void main(String args[]){
          try{
             int a[] = new int[2];
             System.out.println(a[3]);
          }catch(ArrayIndexOutOfBoundsException e){
             System.out.println("Exception thrown  :" + e);
          }
          System.out.println("Out of the block");
       }
    }
    

    抛出异常

    对于非运行时系统的异常,当定义的方法可能产生异常时,程序员必须抛出可能的异常。

    • throw

    throw语句用于抛出一个异常。

    异常是某个异常类的对象,当有了异常类的实例之后就可以用throw语句抛出。

    public class m1 {
        public static void main(String [] args){
            Student stu1 = new Student();
            stu1.deposit();
        }
    }
    class Student{
        public void deposit(){
            throw new ArithmeticException();
        }
    }
    
    • throws

    为了指明在你定义的方法体内的某些代码可能会产生异常,可以在定义方法时使用throws关键字,并带有该方法可能包含异常的名字。

    public class m1 {
        public static void main(String [] args){
            Student stu1 = new Student();
            stu1.deposit();
        }
    }
    class Student{
        public void deposit() throws ArrayIndexOutOfBoundsException{//数组越界异常
            int a[] = new int[2];
            System.out.println(a[3]);
        }
    }
    

    用户自定义异常

    对于某个程序特有的错误,需要编程人员根据程序自身逻辑在用户程序中自己创建异常类和异常对象。

    用户定义的异常类必须是Throwable的子类,Java推荐以Exception类作为直接父类创建用户异常类。

    public class m1 {
        public static void main(String [] args) throws Demo1{
            throw new Demo1();
        }
    }
    class Demo1 extends Exception{//用户自定义异常
        Demo1(){
            System.out.println("Exception occured");
        }
    }
    
  • 相关阅读:
    队列的定义与实现(C语言实现)
    在CTime类中重载<<和>>
    华为OJ:统计大写字母个数
    sql server smo
    应用服务器负载平衡集群
    存储过程如何执行的快速
    sql server 分布式事务
    代理服务器
    怎样实现数据库负载均衡集群
    多层插件开发框架
  • 原文地址:https://www.cnblogs.com/1911087165zzx/p/13124787.html
Copyright © 2020-2023  润新知