zoukankan      html  css  js  c++  java
  • Java基础(七)--Exception异常处理

    发现错误的理想时机是程序运行之前(编译期),然后不太现实,很多异常无法被发现(特别是业务上的数据),需要在运行时解决。

      错误恢复机制保证代码健壮性的方式,异常处理在程序中很常见,也是必须的,必须考虑有可能发生的异常,才能保证程序的正常运行。而且

    一旦程序出现异常,异常处理及日志能帮助我们定位和解决异常。

    概念:

      Exception,是一种意外,不正常的现象。使用异常可以降低处理代码的复杂度,如果不适用异常,就必须通过判断去检查错误,而且可能在很

    多地方都要判断,使用异常,只需要在一个地方处理错误,可以节省代码,能够把正常程序运行和异常分开。

    异常分为Error和Exception,继承Throwable

    1、Error:无法处理的异常,是一种错误,最常见的就是OutOfMemoryError,jvm直接停止运行

    2、Exception:一般性的异常,只要能够捕捉处理,就能保证程序正常运行,比如NullPointerException、IndexOutOfBoundsException

    Exception又分为:

    1、运行时异常RuntimeException:

      我们将RuntimeException或其他继承自RuntimeException的子类称为unchecked Exception。RuntimeException即使不编写异常处理的程序

    代码,依然可以编译成功。这种异常在程序运行过程中可能出现,例如NullPointException、ClassCastException等

    2、非运行时异常RuntimeException:

      其他继承自Exception异常的子类称为checked Exception,编译器强制要求进行捕获处理,比如常见的IOExeption、SQLException,否则编

    译无法通过

    如何处理异常?

    异常处理方式常用的有两种,分别如下:

    1、try、catch、finally

    public static void main(String[] args) {
    	try {														//要检查的程序语句
    		int i = 3/0;
    		System.out.println("test");
    	}} catch (ArithmeticException e) {
            System.out.println("ArithmeticException");
        } catch (Exception e) {												//异常发生时的处理语句
    		System.out.println("Exception Message: " + e.getMessage());
    		throw e;
    	} finally {													//肯定会执行的部分,无论是否发生异常
    		System.out.println("finally Handler");
    	}
    }

    结果:

    ArithmeticException
    finally Handler

    总结:

      1、finally、catch都是可以省略的

      2、catch可以有多个,如果没有异常,不会执行,发生异常的话,按照顺序匹配,如果匹配,就不会与后面的catch块匹配

      3、finally无论如何都会执行,即使之前有return语句。通常在finally进行资源释放的代码,或者lock的解锁,某些关键日志的保存等业务场景

    PS:不要在finally使用return,因为会覆盖之前的return语句,很容易造成混淆

      相比throws更加灵活,更好的控制程序流程

    2、throw、throws

    public int add(int i) throws Exception {
    	if (i == 0) {
    		throw new IllegalArgumentException();
    	}
    	return 0;
    }

      throws:是把异常交给jvm进行处理,把异常往上层抛出,一旦发生最终的结果可能就会程序终止(如果上层方法不进行try catch),可以抛出

    多个异常,一般需要在上层进行try catch块进行处理。

      throw:用于主动抛出异常,throw关键字可以写在任何地方,通常和业务有关,通常这个异常时自定义和业务相关的Exception类

    总结和建议:

    1、父类或接口,对于子类是实现类的限制:

      1.1).无论是继承还是实现,父类或者接口没有抛出异常,实现类或子类不能抛出异常

    public class A {
    
        public void f1()  {
    
        }
    }
    public class B extends A{
    
        @Override
        public void f1() throws IOException{
            System.out.println("");
        }
    }

      1.2).父类或者接口抛出异常Exception1,实现类或子类可以是否抛出异常都可以,如果抛出Exception2,不能是Exception1的父类

    public class A {
    
        public void f1()  throws IOException{
    
        }
    }
    public class B extends A{
    
        @Override
        public void f1() throws Exception{
            System.out.println("");
        }
    }

      1.3).父类的方法抛出异常只有非运行时异常(运行时异常),则子类在重写该方法的时候声明的异常也只能有非运行时异常(运行时异常),

    不能含有运行时异常(非运行时异常)。

    public class A {
    
        public void f1()  throws IOException{
    
        }
    }
    public class B extends A{
    
        @Override
        public void f1() throws IOException,ClassNotFoundException{
            System.out.println("");
        }
    }

    PS:类和接口在这方面的限制是相同的

    2、throws一定是具体的Exception,而不是直接抛出Exception,否则上层必须也是抛出Exception,无法定位具体的异常

    3、多catch块的异常,一定是小异常在前面,否则可能永远无法捕捉到

    4、谨慎使用异常,因为会影响程序性能,能用判断解决还是要判断的

    5、不要使用空的catch块

    6、异常处理尽量抛到最上层进行统一处理

    内容参考:Java异常处理和设计和《Java异常处理》

  • 相关阅读:
    Java架构师必知:什么是单点登录,主要会应用于哪些场景?
    15年老程序员首次分享,年薪200万是怎么做到的
    架构师进阶之路,什么人适合当架构师
    阿里大咖首次分享:软件工程危机真来了?
    用【快餐店】理解日均亿级高并发分布式架构,秒懂!
    Semi-supervised learning for Text Classification by Layer Partitioning
    Ordered Neurons: Integrating Tree Structures Into Recurrent Neural Networks
    Deep Learning Recommendation Model for Personalization and Recommendation Systems
    条件随机场
    隐马尔可夫模型
  • 原文地址:https://www.cnblogs.com/huigelaile/p/11015151.html
Copyright © 2011-2022 走看看