gpt4 book ai didi

c# - 重新抛出 ex.InnerException 是个坏主意?

转载 作者:行者123 更新时间:2023-11-30 16:36:16 25 4
gpt4 key购买 nike

基本上,我的问题简短而有趣:以下是一个坏主意吗(封装并重新抛出 ex.InnerException 而不是 ex)

(有一个类似的问题 here,但不完全...我想重新封装 InnerException,因此保留堆栈跟踪而不反射(reflect)内部结构)

public abstract class RpcProvider
{
public virtual object CallMethod(string methodName, params object[] parameters)
{
MethodInfo mi = this.GetType().GetMethod(methodName);

if (mi == null || mi.GetCustomAttributes(typeof(RpcCallAttribute), true).Length == 0)
{
throw new NotImplementedException("This method is not provided by this RPC provider.");
}
else
{
try
{
return mi.Invoke(this, parameters);
}
catch (TargetInvocationException ex)
{
throw new RpcException("There was an error in the RPC call. See the InnerException for details.", ex.InnerException);
}
}
}
}

下面的堆栈跟踪看起来完好无损(好吧,它没有反射调用方法的内部结构),所以这有什么问题吗?为了使下面的堆栈跟踪有意义,我的继承层次结构是:

 -Oxide.Net.Rpc.RpcProvider |-Oxide.Net.Rpc.XmlRpc  |-StartMenuSorter.DesktopMasters(sanitised to protect the innocent, ie. me)at Oxide.Net.Rpc.XmlRpc.DoRequest(Uri rpcConnectionUri, IXPathNavigable request, String userAgent) in \Projects\OxideLib\Oxide.Net\Rpc\XmlRpc.cs:line 243at StartMenuSorter.DesktopMasters.GetIconInformation(IEnumerable`1 icons) in \Projects\StartMenuSorter\StartMenuSorter\DesktopMasters.cs:line 17at Oxide.Net.Rpc.RpcProvider.CallMethod(String methodName, Object[] parameters) in \Projects\OxideLib\Oxide.Net\Rpc\RpcProvider.cs:line 52at StartMenuSorter.Program.Main() in \Projects\StartMenuSorter\StartMenuSorter\Program.cs:line 36at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()at System.Threading.ThreadHelper.ThreadStart_Context(Object state)at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)at System.Threading.ThreadHelper.ThreadStart()

最佳答案

看起来完全合理:堆栈跟踪包括 RpcProvider 调用方法的位置的详细信息,并隐藏了血腥和不必要的反射 gubbins,所以你没问题。

与所有错误处理代码一样,最终消费者将是其他开发人员,因此最好问问“如果出现问题,我是否有足够的细节来自行调试?”

由于没有像抛出外部异常那样的干扰,因此该技术可以很好地发挥作用。

关于c# - 重新抛出 ex.InnerException 是个坏主意?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1046917/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com