0

I'm using a windows service and I would like to catch all of the inner exceptions in my root method. In one of my inner methods, I'm using a try/catch block like below. When an error occurs, the service doesn't return the error message to my root method. Instead it just throws an error and stops inside my inner method.

How do I return the error message to my root method ?

try
{
    loadAdapterProperties(ref strAssemblyName, ref strTypeName, ref statusXML,
       strAssignedTask, ref lastAdapter, ref entityTypeCode, ref adminID);

    if (strAssemblyName == "" || strTypeName == "")
    {
        LogWriter.WriteTimedMessage("No adapters currently available, {0}",
            strAssignedTask);                         
        loopCondition = false;
        break;
    }

    objHandle = Activator.CreateInstance(strAssemblyName, strTypeName);
    objAdapter = (BaseGatesAdapter)objHandle.Unwrap();

    objAdapter.Load(strAssignedTask, statusXML, entityTypeCode, adminID);
    objAdapter.Execute();
    LogWriter.WriteTimedMessage("Executed {0}", strAssemblyName);
    if (lastAdapter == true)
        loopCondition = false;
}
catch (System.Exception e)
{
    loopCondition = false;                     
    LogWriter.WriteTimedMessage("FAILED to execute {0}",strAssemblyName);
    throw;
}
4

1 回答 1

1

您可能的意思是运行应用程序时收到的错误消息指向内部方法。这意味着(很可能)您没有在根级别执行 try/catch。

这可能很棘手 - 如果您在机器上安装了调试器,调试器将在您的主应用程序之前收到有关此“抛出”的通知。根据调试器配置,它可能会暂停您的应用程序。请参阅此处了解如何完全关闭它: 抑制第一次机会异常

这是关于如何在线程级别注册异常事件的说明,因此您不必在所有方法上包装 try/catch 如何在 .NET 应用程序中捕获所有异常/崩溃

于 2013-09-27T18:42:51.973 回答