java try catch problem

Controller function layer using try catch

. 1    public the Wrapper billQuery (@RequestBody OrderBillQueryReq orderBillQueryReq) {
 2          log.info ( "bill into a list query parameters: {}" , JSON.toJSONString (orderBillQueryReq));
 . 3          the try {
 . 4              PageList <OcOrderBillInfoDto> billList =   orderBillService.list (orderBillQueryReq );
 . 5              return WrapMapper.ok ( new new PageVO <> (billList));
 . 6          } the catch (Exception E) {
 . 7              log.error ( "query statement list fails, an error message: {}" , e.getMessage (), E );
 . 8              return WrapMapper.error (e.getMessage ());
 . 9          }
10     }
11 }    

 

Whether to use the influence of try catch function Controller layer ServiceImpl layer

1, using try catch function, caught Exception

 1 public PageList<OcOrderBillInfoDto> list(OrderBillQueryReq orderBillQueryReq) {   
 2     try {
 3         List<Integer> listIN = new ArrayList<>();
 4         listIN.get(2);
 5     } catch (Exception e) {
 6         log.error("错误信息:{}", e.getMessage(),e);
 7     }
 8 
 9     //业务逻辑
10     return new PageList<>(listModel,pageList.getPaginator());
11 }

The results: service layer will print the error log, Controller layer does not print an error message and logs. However, the following services will continue to implement the code, the result of normal output to the front end.

1 [service-xx-bill] 2020-04-08 10:27:27.932 [http-nio-9100-exec-1] ERROR c.w.s.c.bill.service.impl.OrderBillServiceImpl - 错误信息:Index: 2, Size: 0
2 java.lang.IndexOutOfBoundsException: Index: 2, Size: 0
3     at java.util.ArrayList.rangeCheck(ArrayList.java:657)
4     at java.util.ArrayList.get(ArrayList.java:433)
5     at xxxxxxxl.OrderBillServiceImpl.list(OrderBillServiceImpl.java:56)
6     at xxxxxxx.controller.OrderBillController.billQuery(OrderBillController.java:59)
7     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

2, do not use try catch function.

1 public PageList<OcOrderBillInfoDto> list(OrderBillQueryReq orderBillQueryReq) {   
2     List<Integer> listIN = new ArrayList<>();
3     listIN.get(2);
4     
5     //业务逻辑
6     return new PageList<>(listModel,pageList.getPaginator());
7 }

The results: service layer does not print the error log, Controller layer print an error message and logs. The following code will not continue with the business, output an error message to the front end.

1 [xxxx-bill] 2020-04-08 10:38:37.259 [http-nio-9100-exec-2] ERROR c.w.s.c.bill.controller.OrderBillController - 查询账单列表失败,错误信息:Index: 2, Size: 0
2 java.lang.IndexOutOfBoundsException: Index: 2, Size: 0
3     at java.util.ArrayList.rangeCheck(ArrayList.java:657)
4     at java.util.ArrayList.get(ArrayList.java:433)
5     at xxxx.bill.service.impl.OrderBillServiceImpl.list(OrderBillServiceImpl.java:55)
6     at xxxx.bill.controller.OrderBillController.billQuery(OrderBillController.java:59)
7     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

 

Guess you like

Origin www.cnblogs.com/sqy123/p/12658448.html