In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
How to throw a custom exception in Dubbo? for this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.
The custom exception on the provider side of the dubbo service becomes RuntimeException on the consumer side, so that the custom exception cannot be caught.
The dubbo source code has a unified exception handling strategy:
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;}
As can be seen from the source code, there are several situations in which the exception is returned directly
An Exception is thrown instead of a RuntimeException
There is a declaration on the method signature
Exception class and interface declaration are in the same package
It's an exception of JDK itself.
It's an exception of dubbo itself.
From the above situations, we can see that there are the following solutions
Throws custom exception on method signature
Put exception classes and interface declarations in the same class (recommended for new projects and small project modifications)
Override the exception handling class ExceptionFilter of dubbo
This is the answer to the question about how to throw a custom exception in Dubbo. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.