• java基础:12.1 异常处理


    异常处理的优点:它能使方法抛出一个异常给它的调用者,由调用者处理该异常。

                                                         try catch finally throws 

    1、一般形式

    throw new ArithmeticException("XXX");           //  java.lang.ArithmeticException
    // 在方法中使用。当异常被抛出,正常的执行流程被中断

    try{                    //A statement or a method that may throw an exception;
            statements;     // code to run           
    }

    catch(type ex){        //    Code to process the exception ;
            handling ex;                     
            throw  ex;    
    // 如果异常处理器不能处理一个异常,或者只是简单地希望它的调用者注意到该异常,Java 允许该异常处理器重新抛出异常。
    }

    finally{
            finalStatements;
    }     
    //不要用try-case块处理简单、可预料的情况!!!

    2、finally

    在任何情况下,finally块中的代码一定会被执行,不论try块中是否出现异常或者是否被捕获。考虑下面三种可能出现的情况:

    1. 如果try块中没有出现异常,执行finalStatements,然后执行try语句的下一条语句。
    2. 如果try块中有一条语句引起异常,并被catch捕获,然后跳过try块的其他语句,执行catch块和finally子句。执行try语句之后的下一条语句。
    3. 如果try块中有一条语句引起异常,但是没有被任何catch块捕获,就会跳过try块中的其他语句,执行finally子句,并且将异常传递给这个方法的调用者。

    即使在到达finally块之前有一个return语句,finally块还是会执行。

    2、异常的根类是java.lang.Throwable

    3、所有的java异常类都直接或者间接地继承自Throwable。可以通过继承Exception或者Exception的子类来创建自己的异常类。

    异常类有三种主要类型:系统错误(system error)、异常(exception)和运行时异常(runtime exception)

    4、系统错误(system error)是由java虚拟机抛出的,用Error类表示。Error类描述的是内部系统错误,这样的错误很少发生。如果发生,除了通知用户以及尽量稳妥地终止程序外,几乎什么也不能做。

    5、运行时异常( runtime exception) 是用RuntimeException 类表示的,它描述的是程序设计错误,例如,错误的类型转换、访问一个越界数组或数值错误。运行时异常通常是由Java 虚拟机抛出的。

    6、RuntimeException 、Error 以及它们的子类都称为免检异常( unchecked exception)。所有其他异常都称为必检异常( checked exception), 意思是指编译器会强制程序员检査并通过trycatch块处理它们,或者在方法头进行声明。

    Exceprion这个层次结构又分解为连个分支:一个分支派生于RuntimeException;另一个分支包含其他异常。划分两个分支的规则是:由程序错误导致的异常属于RuntimeException;而程序本身没有没有问题,但由于像I/O错误这类异常导致的异常属于其他异常。 

    常见的RuntimeException(运行时异常): 

    IndexOutOfBoundsException(下标越界异常) 

    NullPointerException(空指针异常) 

    NumberFormatException (String转换为指定的数字类型异常) 

    ArithmeticException -(算术运算异常 如除数为0) 

    ArrayStoreException - (向数组中存放与声明类型不兼容对象异常) 

    SecurityException -(安全异常) 

    IOException(其他异常) 

    FileNotFoundException(文件未找到异常。) 

    IOException(操作输入流和输出流时可能出现的异常。) 

    EOFException (文件已结束异常)

     

    7、如果方法没有在父类中声明异常,那么就不能再子类中对其进行继承来声明异常。

    8、在catch块中异常被指定的顺序是非常重要的,如果父类的catch块出现在唉子类的catch块之前,就会导致编译错误。

    修改我们之前的类Circle

    // Circle.java
    public class Circle {
        private double radius;
        private static int circleNumber =0;
        
        /*	set radius */
        Circle(){
            circleNumber++;
            radius = 1.0;
        }
        Circle(double newRadius){
            setRadius(newRadius);
            circleNumber++;
        }
        /* return radius */
        public double getRadius() {
        	return radius;
        }
        /* set radius 
        public void setRadius(double radius) {	
        	this.radius = (radius >=0) ? radius : 0;
        } */   
        public void setRadius(double radius) throws IllegalArgumentException{	
    /* 即使在方法声明中删除throws IllegalArgumentException 子句,
    Circle类也仍然会编译,因为该异常是RuntimeException 的子类,
    而且不管是否在方法头中声明,每个方法都能抛出RuntimeException 异常(免检异常)。*/
        	if (radius>=0)  
        		this.radius=radius;
        	else throw new IllegalArgumentException("radius cannot be negative");
        } 
        /* get the number of all circles */
        public static int getNumberOfCircle() {
        	return circleNumber;
        }
        public double getArea(){
            return radius*radius*Math.PI;
        }
    }

    测试程序: 

    //  testCircle.java
    public class testCircle {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		try {
    		Circle c1 = new Circle(-2);
    		Circle c2 = new Circle(5);
    		Circle c3 = new Circle();
    		Circle c4 = new Circle(0);
    		}
    		catch (IllegalArgumentException ex) {
    			System.out.println(ex);
    		}
    		System.out.println("the number of circle is : " + Circle.getNumberOfCircle());
    	}
    }
    

    观察程序的运行结果,我们发现:当c1出现异常,成功catch后,不会再返回执行Circle c2 = new Circle(5)以及后续语句;程序会顺着catch块后继续执行。

    basic knowledge:

    在执行完catch 块之后,程序控制不返回到throw 语句;而是执行catch 块后的下一条语句。

    自定义异常

    //  DefineException.java
    public class DefineException extends Exception {
    	private double radius;
    	public DefineException(double radius) {
    		super("Invaild radius "+radius);
    		this.radius= radius;
    	}
    	public double getRadius() {
    		return radius;
    	}
    }
    // Circle.java
    public class Circle {
        private double radius;
        private static int circleNumber =0;
        
        /*	set radius */
        Circle() throws DefineException {
            circleNumber++;
            radius = 1.0;
        }
        Circle(double newRadius) throws DefineException{
            setRadius(newRadius);
            circleNumber++;
        }
    
        /* return radius */
        public double getRadius() {
        	return radius;
        }
        public void setRadius(double radius) throws DefineException{	
        	if (radius>=0)  
        		this.radius=radius;
        	else throw new DefineException(radius);
        }
        
        /* get the number of all circles */
        public static int getNumberOfCircle() {
        	return circleNumber;
        }
        
        public double getArea(){
            return radius*radius*Math.PI;
        }
    }
    
    
    //  testCircle.java
    public class testCircle {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		try {
    		Circle c1 = new Circle(-2);
    		Circle c2 = new Circle(5);
    		Circle c3 = new Circle(6);
    		Circle c4 = new Circle(0);
    		}
    		catch (DefineException ex) {
    			System.out.println(ex);
    		}
    		finally {
    			System.out.println("finally");
    		}
    
    		System.out.println("the number of circle is : " + Circle.getNumberOfCircle());
    	}
    }
    
  • 相关阅读:
    适配器模式—对象适配器模式
    状态模式
    抽象工厂模式、反射
    Spring日记_01 之 Maven搭建
    既有e^x又有sinx或cosx的积分题的解法
    printf的执行顺序
    C++ 冒泡排序、选择排序、快速排序
    神舟战神 HotKey 主面板无法打开? Fn+Esc失效?
    PyCharm 和 IntelliJ IDEA的破解激活 、添加文件头注释
    单片机 之 超声波测距
  • 原文地址:https://www.cnblogs.com/l20902/p/10610946.html
Copyright © 2020-2023  润新知