[Java基础]Java异常捕获
在Java中,异常分为二类:Error和Exception, Exception是运行时异常,Error是JVM内部错误,捕获Exception没法捕获Error
Throwable
| |
Error Exception
|
RuntimeException
今天我在项目中就遇到了一个问题,如下:
try { //更新log PullUpdateLogVO vo = new PullUpdateLogVO(); vo.setNotifyId(notification.getNotifyId()); vo.setQueryStatus(pullDataResult != null ? pullDataResult.getStatus() : 3); vo.setQueryTime(queryTime); vo.setRespSerial(pullDataResult != null ? pullDataResult.getRespSerial() : ""); vo.setUserid(notification.getUserid()); String res = getValidateService().pullUpdate(vo); logger.info(String.format("更新任务查询状态成功: log = %s", res)); } catch (Exception ex) { logger.warn(String.format("更新任务查询状态失败:ex=%s", ex.getMessage())); }
打印出异常信息,如下图所示:
Exception in thread "Mq-MqQueueExcutorServiceNew-DataChannel-PullDataNotify-104-3" java.lang.NoSuchMethodError: com.ppdai.cbd.thirdparty.pulldata.common.request.PullUpdateLogVO.setUserid(Ljava/lang/Integer;)V at com.ppdai.realtime.datachannel.pullservice.PullNotificationDispatchService.dispatchNotification(PullNotificationDispatchService.java:122) at com.ppdai.realtime.datachannel.mq.service.MessageQueueConsumer.onMessageReceived(MessageQueueConsumer.java:51) at com.ppdai.infrastructure.mq.client.biz.MqQueueExcutorService.doMessageReceived(MqQueueExcutorService.java:461) at com.ppdai.infrastructure.mq.client.biz.MqQueueExcutorService.doDealMsg(MqQueueExcutorService.java:408) at com.ppdai.infrastructure.mq.client.biz.MqQueueExcutorService.handleMessage(MqQueueExcutorService.java:369) at com.ppdai.infrastructure.mq.client.biz.MqQueueExcutorService.access$600(MqQueueExcutorService.java:48) at com.ppdai.infrastructure.mq.client.biz.MqQueueExcutorService$3.run(MqQueueExcutorService.java:308) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748)
NoSuchMethodError就是典型的JVM内部错误,属于Error的范畴,一般这种问题编译器就应该解决,Java Class编译文件class文件函数找不到错误。Error类层次如下:
java.lang.Throwable
java.lang.Error
java.lang.LinkageError
java.lang.IncompatibleClassChangeError
NoSuchMethodError
解决办法:1.将捕获Exception改成捕获Throwable 2.另一种将依赖包类添加对应方法
欢迎关注Java流水账公众号