zoukankan      html  css  js  c++  java
  • 多catch块的代码优化

    一、多catch块的代码优化

    在写代码时,多行存在不同的异常,使用try catch的话,习惯性的是有多个catch,如下所示:

    注意到warning,文字描述如下:

    Reports identical catch sections in try blocks under JDK 7. A quickfix is available to collapse the sections into a multi-catch section.
    This inspection only reports if the project or module is configured to use a language level of 7.0 or higher.

     大概意思是再try的代码块中,存在多个catch块结构时,如果使用的是JDK 7及以上,把这些catch块进行折叠到一个中更高效,如下所示:

    二、&&、&、|、||的区别

    另外,扩展一下&&、&、|、||的区别:简单来说就是&&,||有短路功能,而&、|没有,所以&&、||的性能更佳,测试代码如下:

        @Test
        public void test() {
            int c = 2;
            if (c == b() | c == a()) {
                // 会打印
                // b() 返回值: 2
                // a() 返回值: 1
                System.out.println("|  c = " + c);
            }
            System.out.println("|---- test end");
    
    
            if (c == b() || c == a()) {
                // 会打印
                // b() 返回值: 2
                System.out.println("||  c = " + c);
            }
            System.out.println("||---- test end");
    
    
            if (c == a() & c == b()) {
                // 会打印
                // a() 返回值: 1
                // b() 返回值: 2
                System.out.println("&  c = " + c);
            }
            System.out.println("&---- test end");
    
    
            if (c == a() && c == b()) {
                // 会打印
                // a() 返回值: 1
                System.out.println("&&  c = " + c);
            }
            System.out.println("&&---- test end");
    
        }
    
        public int a() {
            System.out.println("a() 返回值: " + 1);
            return 1;
        }
    
        public int b() {
            System.out.println("b() 返回值: " + 2);
            return 2;
        }
  • 相关阅读:
    php数组函数array_slice、array_splice
    php使用curl_init()和curl_multi_init()多线程的速度比较详解
    mysql忘记root密码
    php的RSA非对称加密
    phpstudy开启xdebug
    Centos7系统yum安装软件 No more mirrors to try
    python数据分析与数据可视化入门级
    第一周博客总结
    python——pandas初体验
    第十六周博客总结
  • 原文地址:https://www.cnblogs.com/miaoying/p/10845665.html
Copyright © 2011-2022 走看看