zoukankan      html  css  js  c++  java
  • Java检查型异常和非检查型异常

    1.代码

    public class ExcepTest {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            System.err.println("111111111111");
    //        say();  //抛出异常,但是代码不显示的try..catch..
            
    //        calc(); //抛出异常,但是代码不显示的try..catch..
            
    //        try {
    //            show();
    //        } catch (Exception e) {
    //            e.printStackTrace();
    //            System.err.println("异常成功捕获。。");
    //        }
            
    //        try {
    //            view();
    //        } catch (Exception e) {
    //            e.printStackTrace();
    //            System.err.println("异常成功捕获。。");
    //        }
            
            calc22();
            System.err.println("222222222222");
        }
        
        public static void say(){            //new NullPointerException
            System.err.println("say>>>>");
            throw new NullPointerException();
        }
        
        public static void calc() {           //throws ArithmeticException ,这个异常不提示。
            System.err.println("calc>>>>");
            int i = 0, j = 0;
            System.err.println( i/j );
        }
        
        public static void show() throws Exception{  //这个异常提示。
            System.err.println("show>>>>");
            int i = 0, j = 0;
            System.err.println( i/j );
        }
        
        public static void view() throws Exception{
            System.err.println("view>>>>");
            throw new Exception();
        }
        
        public static void calc22(){
            System.err.println("calc22>>>>");
            try {
                int i = 0, j = 0;
                int k = i/j ;
            } catch (Exception e) {
                System.err.println("异常处理完毕。。。。");
    //            throw e;  //异常再度抛出。
    //            throw new ArithmeticException();   //继承自exception的则是检查型异常(当然,runtimeexception本身也是exception的子类
    //            throw new RuntimeException();  //(当然,runtimeexception本身也是exception的子类
                //继承自runtimeexception或error的是非检查型异常
                //ArithmeticException继承RuntimeException
                
            }
            System.err.println("calc22>>>>calc22");
        }
    }

    2.说明
    使用spring难免要用到spring的事务管理,要用事务管理又会很自然的选择声明式的事务管理,在spring的文档中说道,spring声明式事务管理默认对非检查型异常和运行时异常进行事务回滚,而对检查型异常则不进行回滚操作。
    那么什么是检查型异常什么又是非检查型异常呢?
    最简单的判断点有两个:
    1.继承自runtimeexception或error的是非检查型异常,而继承自exception的则是检查型异常(当然,runtimeexception本身也是exception的子类)。
    2.对非检查型类异常可以不用捕获,而检查型异常则必须用try语句块进行处理或者把异常交给上级方法处理总之就是必须写代码处理它。
    引用:http://blog.csdn.net/yixiaoqingyuz/article/details/4485606

  • 相关阅读:
    python 操作 mysql 数据库 datetime 属性字段为 0000-00-00 00:00:00 的问题
    git 学习笔记
    SB Admin 2 学习笔记1
    docker容器与容器云读书笔记1
    urlencode遇到中文编码问题
    flask_sqlalchemy 乱码问题
    centos6u3 安装 celery 总结
    flask_sqlalchemy 命名遇到的一个小坑
    使用 bash 创建定时任务
    tornado 异步调用系统命令和非阻塞线程池
  • 原文地址:https://www.cnblogs.com/simpledev/p/3984410.html
Copyright © 2011-2022 走看看