• java-异常进阶-包的使用


    一 finally

    1.1 异常执行的顺序

    package test;
    
    public class Test {
    
        public static void main(String[] args)  {
        Demo d = new Demo();
        try {
            d.show(-5);
            System.out.println("hello try");
        } catch (NoShowException e) {
            e.printStackTrace();
            System.out.println("hello catch");
        }
        }
    
    }
    class NoShowException extends Exception
    {
        NoShowException(String message)
        {
            super(message);
        }
    }
    
    class Demo
    {
        void show(int num)throws NoShowException
        {
            if(num<0)
                throw new NoShowException(num+",数值是非法的");
            System.out.println("show ....."+num);
        }
    }

    结果:

    1.2 finally 作用

    有一些特定的代码无论异常是否发生,都需要执行。
    因为异常会引发程序跳转,导致有写语句执行不到。无法满足这个需求。
    异常捕获处理时java提供解决方案。

    应用场景;

    定义一个功能往数据库中添加数据。
    
    void add(Data data)throws NoAddException
    {
    //1,连接数据库。
    try{
    //2,添加数据。//添加数据时发生了异常情况。throw new SQLException();程序跳转,就执行不到断开连接。
    //而断开连接必须要执行,因为不执行,连接资源在浪费。
    //无论是否发生问题,都需要执行断开连接的动作,从而释放资源。
    }catch(SQLException e)
    {
    //解决数据库的问题。
    //同时将问题告诉调用者。
    throw new NoAddException();
    }
    finally
    {
    //3,断开连接。
    }
    
    }

    总结:finally到底什么时候用
    只要程序中使用到了具体的资源(数据库连接,IO资源,网络连接socket等)
    需要释放,都必须定义在finally中。你在定义程序,只要问题发生与否,
    指定程序都需要执行时,就定义finally中。

    二 异常的组合方式


    try catch finally 组合方式:

    1 .try catch : 对代码进行异常检测,并对检测的异常传递给catch处理。

    try catch : 对代码进行异常检测,并对检测的异常传递给catch处理。
    异常捕获处理。
    void show()//不用throws 
    {
    try{
    throw new Exception();
    }catch(Exception e)
    {
    
    }
    }

    2. try finally : 对代码进行异常检测,检测到异常后因为没有catch,所以一样会被默认jvm抛出。
    异常是没有捕获处理的。但是功能所开启资源需要进行关闭,所有finally。
    只为关闭资源。

    void show()//需要throws 
    {
    try{
    throw new Exception();
    }finally
    {
    
    }
    }

    3,
    try catch finally
    检测异常,并传递给catch处理,并定义资源释放。

    4,try catch1 catch2 catch3...... 

    class ExceptionDemo10 
    {
    public static void main(String[] args) 
    {
    System.out.println("Hello World!");
    }
    }

    三 异常在继承或实现的细节

    异常在继承或者实现中的使用细节:★★★★★
    1,子类覆盖父类方法时,如果父类的方法声明异常,子类只能声明父类异常或者该异常的子类,或者不声明。
    2,当父类方法声明多个异常时,子类覆盖时只能声明多个异常的子集。
    3,当被覆盖的方法没有异常声明时,子类覆盖时时无法声明异常的。
    举例:父类存在这种情况,接口也有这种情况,
    问题:接口中没有声明异常,而实现的子类覆盖方法时发生了异常,怎么办?
     无法进行throws声明,只能catch的捕获。万一问题处理不了呢?catch中继续throw抛出,但是只能将异常转换成RuntimeException子类抛出

    1,子类覆盖父类方法时,如果父类的方法声明异常,子类只能声明父类异常或者该异常的子类,或者不声明。

    原因:如是不是这样,会发生类型转换异常.具体看下面代码讲解

    下面红色部分产生类型转换异常 //AException ex = new BException();

    ps:父类有问题了,在覆盖的时候子类不能有更多的问题

    Exception
        |--AException
            |--AAException
        |--BException
    
    class AException extends Exception
    {
    }
    class BException extends Exception
    {
    }
    class AAException extends AException
    {
    }
    
    
    class Fu
    {
        void show()
        {
            
        }
    }
    
    class Tool
    {
        void method(Fu f)//Fu f = new Zi();
        {
            try
            {
                f.show();
            }
            catch (AException ex)//AException ex = new BException();
            {
            }
        }
    }
    Tool t = new Tool();
    //t.method(new Fu());
    t.method(new Zi());
    
    
    class Zi extends Fu
    {
        void show()throws BException
    { } }

     四 习题

    1.
    写出程序结果
    class Demo
    {    
        public static void func()//throws Exception
        {
            try
            {
                throw  new Exception();//抛出编译时异常,并没有catch处理,必须throws声明。
            }
            finally
            {
                System.out.println("B");
            }
        }
        public static void main(String[] args)
        {
            try
            {
                func();
                System.out.println("A");
            }
            catch(Exception e)
            {
                System.out.println("C");
            }
            System.out.println("D");
        }
    }
    
    
    -----------B C D
    2.写出程序结果    
    class Demo
    {
        public static void main(String[] args)
        {
            try
            {
                showExce(); 
                System.out.println("A");
            }
            catch(Exception e)
            {
                System.out.println("B");
            }
            finally
            {
                System.out.println("C");
            }
            System.out.println("D");
        }
        public static void showExce()throws Exception
        {
            throw new Exception();
        }
    }
    B C D
    
    3.写出程序结果:
    class Demo
    {    
        public static void func()
        {
            try
            {
                throw  new Exception();//有throw抛出异常,功能会在这里结束。下面的语句都是废话。
                System.out.println("A");
            }
            catch(Exception e)
            {
                System.out.println("B");
            }
        }
        public static void main(String[] args)
        {
            try
            {
                func();
            }
            catch(Exception e)
            {
                System.out.println("C");
            }
            System.out.println("D");
        }
    }

    B D
    ---------------- 4.写出程序结果 class Exc0 extends Exception{} class Exc1 extends Exc0{} class Demo { public static void main(String[] args) { try { throw new Exc1(); } catch(Exception e)//父类的catch必须放在最下面。否则编译失败。 { System.out.println("Exception"); } catch(Exc0 e) { System.out.println("Exc0"); } } }
    ---------------
    
    5.写出程序结果  @   
    class Test
    { 
        public static String output="";
        public static void foo(int i)
        { 
            try
            { 
                if(i==1)
                    throw new Exception();     
                output+="1"; 
            } 
            catch(Exception e)
            { 
                output+="2"; 
                return; 
            } 
            finally
            { 
                output+="3"; 
            } 
            output+="4"; 
        }
        public static void main(String args[])
        { 
            foo(0);
            System.out.println(output);//134
            foo(1); 
            System.out.println(output); //13423
        }
    } 

    134 13423
    ---------------------- 6.编程题。 建立一个图形接口,声明一个面积函数。圆形和矩形都实现这个接口,并得出两个图形的面积。 注:体现面向对象的特征,对数值进行判断。用异常处理。不合法的数值要出现“这个数值是非法的”提示,不再进行运算。 interface Shape { //获取面积函数。 double getArea(); } class NoValueException extends RuntimeException { NoValueException(String message) { super(message); } } class Rec implements Shape { private int length; private int width; Rec(int length,int width) { if(length<=0 || width<=0) throw new NoValueException("非法数值"); this.length = length; this.width = width; } public double getArea() { return length*width; } } class Circle implements Shape { private int radius; private static final double PI = 3.14; Circle(int radius) { this.radius = radius; } public double getAge() { return radius*radius*PI; } } ---------------------- 7. 补足compare函数内的代码,不许添加其他函数。 class Circle { private static double pi=3.14; private double radius; public Circle(double r) { radius=r; } public static double compare(Circle[] cir) { //程序代码 /* 其实就是获取数组中的最大值。 */ // double max = cir[0].radius;// 初始化的是圆数组中的任意一个圆对象的半径。 int maxIndex = 0;//初始化的是数组任意一个角标。 for(int x=1; x<cir.length; x++) { if(cir[x].radius>cir[maxIndex].radius)//if(person.age > person1.age) if(age>age1) { maxIndex = x; } } return cir[maxIndex].radius; } } class TC { public static void main(String[] args) { Circle cir[]=new Circle[3];//创建一个Circle类型的数组,数组有3个元素。Circle[] arr = new Circle[3]; //分别对数组每一个元素进行初始化。 cir[0]=new Circle(1.0); cir[1]=new Circle(2.0); cir[2]=new Circle(4.0); System.out.println("最大的半径值是:"+Circle.compare(cir)); } }


    作者:8亩田
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接.

    本文如对您有帮助,还请多帮 【推荐】 下此文。
    如果喜欢我的文章,请关注我的公众号
    如果有疑问,请下面留言

    学而不思则罔 思而不学则殆
  • 相关阅读:
    2018-2019-2 20175202实验三《敏捷开发与XP实践》实验报告
    20175202 葛旭阳 MyCP
    20175202 《Java程序设计》第九周学习总结
    20175202 《Java程序设计》第八周学习总结
    2018-2019-2 20175202实验二《Java面向对象程序设计》实验报告
    20175202 结对编程练习—四则运算(第一周)
    20175203 2018-2019 实验三 《敏捷开发与XP实践》
    2018-2019-20175203 实验二 《Java面向对象程序设计》
    20175203 2018-2019-2 实验一《Java开发环境的熟悉》实验报告
    20175203 2018-2019-2《Java程序设计》第五周学习总结
  • 原文地址:https://www.cnblogs.com/liu-wang/p/8266485.html
Copyright © 2020-2023  润新知