Dubbo中怎么抛出自定义异常
发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,Dubbo中怎么抛出自定义异常,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。dubbo服务提供端自定义异常,在消费端变成了Runti
千家信息网最后更新 2026年11月01日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安全错误
数据库的锁怎样保障安全
耶加雪菲
南京欣网互联网络科技有限公司
蓝思科技飞鸽互联网
重生之互联网科技帝国
互联网为科技
互联网科技公司怎么样
大白互联网科技
成都狮子座互联网科技
居家互联网科技有限公司
互联网科技类展会
科技互联网的估值
深圳育鹏互联网科技有限公司招聘
中国互联网公司排名锤子科技
洛阳共同富裕互联网科技有限公司
高科技互联网发展前景如何
10位科技互联网大佬
东风日产互联网科技公司叫什么
互联网科技创新方面的资料
利用互联网等科技手段
如何强化互联网金融科技
互联网科技有限公司
喔噻互联网科技
智网互联网科技
科技互联网公司logo
恒生互联网科技指数跳水
畅思互联网科技
互联网科技公司营业执照怎么办理
广州千川互联网科技有限公司
承德科技产业互联网平台
科技互联网企业在行动