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;
        }
  • 相关阅读:
    C#关键字
    委托
    async和await异步编程
    IOC、DI,IoC容器
    数据库脏读、幻读
    关于vue 从零开始的搭建注意事项
    C#.NET WebApi返回各种类型(图片/json数据/字符串)
    win7下IIS配置以及域名映射方法
    18/11月 日常总结
    Js 数组去重的几种方法总结
  • 原文地址:https://www.cnblogs.com/miaoying/p/10845665.html
Copyright © 2011-2022 走看看