Dubbo中怎么抛出自定义异常
发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,Dubbo中怎么抛出自定义异常,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。dubbo服务提供端自定义异常,在消费端变成了Runti
千家信息网最后更新 2025年12月02日Dubbo中怎么抛出自定义异常
Dubbo中怎么抛出自定义异常,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
dubbo服务提供端自定义异常,在消费端变成了RuntimeException,导致无法捕获自定义异常。
dubbo 源码有一个统一的异常处理策略:
public Result invoke(Invoker> invoker, Invocation invocation) throws RpcException { try { Result result = invoker.invoke(invocation); if (result.hasException() && GenericService.class != invoker.getInterface()) { try { Throwable exception = result.getException(); // directly throw if it's checked exception if (!(exception instanceof RuntimeException) && (exception instanceof Exception)) { return result; } // directly throw if the exception appears in the signature try { Method method = invoker.getInterface().getMethod(invocation.getMethodName(), invocation.getParameterTypes()); Class>[] exceptionClassses = method.getExceptionTypes(); for (Class> exceptionClass : exceptionClassses) { if (exception.getClass().equals(exceptionClass)) { return result; } } } catch (NoSuchMethodException e) { return result; } // for the exception not found in method's signature, print ERROR message in server's log. logger.error("Got unchecked and undeclared exception which called by " + RpcContext.getContext().getRemoteHost() + ". service: " + invoker.getInterface().getName() + ", method: " + invocation.getMethodName() + ", exception: " + exception.getClass().getName() + ": " + exception.getMessage(), exception); // directly throw if exception class and interface class are in the same jar file. String serviceFile = ReflectUtils.getCodeBase(invoker.getInterface()); String exceptionFile = ReflectUtils.getCodeBase(exception.getClass()); if (serviceFile == null || exceptionFile == null || serviceFile.equals(exceptionFile)) { return result; } // directly throw if it's JDK exception String className = exception.getClass().getName(); if (className.startsWith("java.") || className.startsWith("javax.")) { return result; } // directly throw if it's dubbo exception if (exception instanceof RpcException) { return result; } // otherwise, wrap with RuntimeException and throw back to the client return new RpcResult(new RuntimeException(StringUtils.toString(exception))); } catch (Throwable e) { logger.warn("Fail to ExceptionFilter when called by " + RpcContext.getContext().getRemoteHost() + ". service: " + invoker.getInterface().getName() + ", method: " + invocation.getMethodName() + ", exception: " + e.getClass().getName() + ": " + e.getMessage(), e); return result; } } return result; } catch (RuntimeException e) { logger.error("Got unchecked and undeclared exception which called by " + RpcContext.getContext().getRemoteHost() + ". service: " + invoker.getInterface().getName() + ", method: " + invocation.getMethodName() + ", exception: " + e.getClass().getName() + ": " + e.getMessage(), e); throw e; }从源码中可以看出,异常直接返回有以下几种情况
抛出的是一个Exception 而非 RuntimeException
在方法签名上有声明
异常类和接口声明在同一个包里
是JDK 自身异常
是dubbo 自身异常
由以上几种情况可以看出可以有以下几种解决方案
在方法签名上throws 自义定异常
将异常类和接口声明放在同一个类中(推荐,适用于新项目和小项目改造)
重写dubbo 的异常处理类ExceptionFilter
关于Dubbo中怎么抛出自定义异常问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注行业资讯频道了解更多相关知识。
方法
问题
情况
接口
更多
源码
处理
帮助
解答
易行
简单易行
内容
小伙
小伙伴
新项目
方案
知识
策略
篇文章
行业
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
python软件开发的前景
第一代网络安全技术
单片机存储数据库
服务器获取键盘硬件信息
摘要认证服务器
PE下载软件开发
吐鲁番软件开发价格走势
数据库数据校验 工具
通信网络安全生产检查项目表
软件开发平台功能清单
游戏开发中客户端与服务器端
dss数据库默认密码
软件开发前景这样
网络安全体现人类命运共同体
金蝶服务器怎么打不开
重邮网络安全的老师
北京综合软件开发参考价格
纸箱吸塑一体软件开发流程
服务器维修网
新网的dns服务器
临沂环保局网络安全
提高软件开发效率应尽量采用
数据库的视图会用到吗
什么叫做存在相同数据库
焦作搜房网络技术有限公司
数据库新技术论文大数据
郑州网络安全防控
网络技术应用教案广教版
一栋高层需要啥路由器服务器
智能手表软件开发流程