Dubbo抛出自定义异常时报:Got unchecked and undeclared exception

时间:2025-03-31 07:17:44
  • public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {  
  •     try {  
  •         Result result = (invocation);  
  •         if (() && GenericService.class != ()) {  
  •             try {  
  •                 Throwable exception = ();  
  •   
  •                 // 如果是checked异常,直接抛出  
  •                 if (! (exception instanceof RuntimeException) && (exception instanceof Exception)) {  
  •                     return result;  
  •                 }  
  •                 // 在方法签名上有声明,直接抛出  
  •                 try {  
  •                     Method method = ().getMethod((), ());  
  •                     Class<?>[] exceptionClassses = ();  
  •                     for (Class<?> exceptionClass : exceptionClassses) {  
  •                         if (().equals(exceptionClass)) {  
  •                             return result;  
  •                         }  
  •                     }  
  •                 } catch (NoSuchMethodException e) {  
  •                     return result;  
  •                 }  
  •   
  •                 // 未在方法签名上定义的异常,在服务器端打印ERROR日志  
  •                 ("Got unchecked and undeclared exception which called by " + ().getRemoteHost()  
  •                         + ". service: " + ().getName() + ", method: " + ()  
  •                         + ", exception: " + ().getName() + ": " + (), exception);  
  •   
  •                 // 异常类和接口类在同一jar包里,直接抛出  
  •                 String serviceFile = (());  
  •                 String exceptionFile = (());  
  •                 if (serviceFile == null || exceptionFile == null || (exceptionFile)){  
  •                     return result;  
  •                 }  
  •                 // 是JDK自带的异常,直接抛出  
  •                 String className = ().getName();  
  •                 if (("java.") || ("javax.")) {  
  •                     return result;  
  •                 }  
  •                 // 是Dubbo本身的异常,直接抛出  
  •                 if (exception instanceof RpcException) {  
  •                     return result;  
  •                 }  
  •   
  •                 // 否则,包装成RuntimeException抛给客户端  
  •                 return new RpcResult(new RuntimeException((exception)));  
  •             } catch (Throwable e) {  
  •                 ("Fail to ExceptionFilter when called by " + ().getRemoteHost()  
  •                         + ". service: " + ().getName() + ", method: " + ()  
  •                         + ", exception: " + ().getName() + ": " + (), e);  
  •                 return result;  
  •             }  
  •         }  
  •         return result;  
  •     } catch (RuntimeException e) {  
  •         ("Got unchecked and undeclared exception which called by " + ().getRemoteHost()  
  •                 + ". service: " + ().getName() + ", method: " + ()  
  •                 + ", exception: " + ().getName() + ": " + (), e);  
  •         throw e;  
  •     }  
  • }