【开发问题解决方案】java.lang.ArithmeticException: / by zero

问题背景:在做查询多条数据结果的柱状图百分比时候,由于有的查询结果值是"0",后台报错如下
java.lang.ArithmeticException: / by zero

问题分析:使用0作为除数导致了报错

大多数报这样的异常是出现在:接口上手写封装分页的Page对象,前端没给传值导致处理出错。

简单的代码示例:

    public static void main(String[] args) {
        int a= 0;
        int b = 5;
        float d = 0;
        double c=0;
        System.out.println(b/d);
        System.out.println(b/c);
        System.out.println(b/a);
    }

结果如下:
【开发问题解决方案】java.lang.ArithmeticException: / by zero_第1张图片
可以看到当"0"是int类型的值时,当做除数会报错:/by zero

业务SQL示例:

SELECT 
  IFNULL(
    (SELECT 
      COUNT(1) 
    FROM
      company 
    WHERE DATE_FORMAT(create_time, '%Y-%m') = '" + month + "' 
      AND zoneId = '" + zoneId + "'),
    0
  ) AS tH,
  IFNULL(
    (SELECT 
      COUNT(1) 
    FROM
      company 
    WHERE DATE_FORMAT(create_time, '%Y-%m') = '" + month + "' 
      AND zoneId = '" + zoneId + "' 
      AND `status` = '1'),
    0
  ) AS pH,
  IFNULL(
    (SELECT 
      COUNT(1) 
    FROM
      company 
    WHERE DATE_FORMAT(create_time, '%Y-%m') = '" + month + "' 
      AND zoneId = '" + zoneId + "' 
      AND `status` = '2'),
    0
  ) AS cH,
  IFNULL(
    (SELECT 
      COUNT(1) 
    FROM
      company 
    WHERE DATE_FORMAT(create_time, '%Y-%m') = '" + month + "' 
      AND zoneId = '" + zoneId + "' 
      AND `status` = '3'),
    0
  ) AS pC,
  IFNULL(
    (SELECT 
      COUNT(1) 
    FROM
      company 
    WHERE DATE_FORMAT(create_time, '%Y-%m') = '" + month + "' 
      AND zoneId = '" + zoneId + "' 
      AND `status` = '4'),
    0
  ) AS cP,
  IFNULL(
    (SELECT 
      COUNT(1) 
    FROM
      company 
    WHERE house_state = '5' 
      AND DATE_FORMAT(create_time, '%Y-%m') = '" + month + "' 
      AND zoneId = '" + zoneId + "'),
    0
  ) AS tJ,
  IFNULL(
    (SELECT 
      COUNT(1) 
    FROM
      company 
    WHERE `water_state` = '5' 
      AND DATE_FORMAT(create_time, '%Y-%m') = '" + month + "' 
      AND zoneId = '" + zoneId + "'),
    0
  ) AS wC,
  IFNULL(
    (SELECT 
      COUNT(1) 
    FROM
      company 
    WHERE electricity_state = '5' 
      AND DATE_FORMAT(create_time, '%Y-%m') = '" + month + "' 
      AND zoneId = '" + zoneId + "'),
    0
  ) AS eC,
  IFNULL(
    (SELECT 
      COUNT(1) 
    FROM
      company 
    WHERE gas_state = '5' 
      AND DATE_FORMAT(create_time, '%Y-%m') = '" + month + "' 
      AND zoneId = '" + zoneId + "'),
    0
  ) AS gC,
  IFNULL(
    (SELECT 
      COUNT(1) 
    FROM
      company 
    WHERE water_state = '3' 
      AND DATE_FORMAT(create_time, '%Y-%m') = '" + month + "' 
      AND zoneId = '" + zoneId + "'),
    0
  ) AS wR,
  IFNULL(
    (SELECT 
      COUNT(1) 
    FROM
      company 
    WHERE electricity_state = '3' 
      AND DATE_FORMAT(create_time, '%Y-%m') = '" + month + "' 
      AND zoneId = '" + zoneId + "'),
    0
  ) AS eR,
  IFNULL(
    (SELECT 
      COUNT(1) 
    FROM
      company 
    WHERE gas_state = '3' 
      AND DATE_FORMAT(create_time, '%Y-%m') = '" + month + "' 
      AND zoneId = '" + zoneId + "'),
    0
  ) AS gR 

   

修改加个if-else判断逻辑

if(monthAnalyse.getWaterCount().equals("0")){
                    monthAnalyse.setWaterRate(0 + "%");
                }else{
                    String waterRate = String.valueOf(new BigDecimal(monthAnalyse.getWaterCount()).divide(new BigDecimal(monthAnalyse.getTotalHouse()), 2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("100")));
                    monthAnalyse.setWaterRate(waterRate + "%");
                }

所以就当我们在处理业务逻辑时,要对除数与被除数进行判断,来规避这样的传参问题。

你可能感兴趣的:(排坑,java,sql)