故障转储显示引发的异常,但有一个 try-catch 应该捕获它

本文关键字:try-catch 有一个 显示 转储 异常 故障 | 更新日期: 2023-09-27 18:37:01

客户报告IIS崩溃并提供故障转储文件。 我无法重现该问题(即使我传递了一个错误的 numStr),并且对到目前为止的发现感到非常困惑。 以下是捕获所有异常的相关代码:

public class NetUtils
{
    public static int stringToInt(string numStr, int defValue)
    {
        int num = defValue;
        try
        {
            num = Int32.Parse(numStr);
        }
        catch (Exception e)
        {
        }
        return num;
    }

这是 Windbg 输出,它告诉我 FormatException 通过了 catch 块。

0:021> !pe
Exception object: 0272e1b8
Exception type: System.FormatException
Message: Input string was not in a correct format.
InnerException: <none>
StackTrace (generated):
<none>
StackTraceString: <none>
HResult: 80131537
0:021> !clrstack
OS Thread Id: 0x1598 (21)
ESP       EIP     
0ffddf80 77e4bee7 [HelperMethodFrame: 0ffddf80] 
0ffde024 79a0e4a8 System.Number.StringToNumber(System.String, System.Globalization.NumberStyles, NumberBuffer ByRef, System.Globalization.NumberFormatInfo, Boolean)
0ffde04c 792ef2d8 System.Number.ParseInt32(System.String, System.Globalization.NumberStyles, System.Globalization.NumberFormatInfo)
0ffde114 02360a3b AXSWebSvc.NetUtils.stringToInt(System.String, Int32)
0ffde140 023624ab AXSWebSvc.AXSExchangeSvc.search(System.String, System.String, System.String, System.String, System.String, System.String, System.String)
0ffde494 79e71b4c [CustomGCFrame: 0ffde494] 
0ffde45c 79e71b4c [GCFrame: 0ffde45c] 
0ffde478 79e71b4c [GCFrame: 0ffde478] 
0ffde65c 79e71b4c [HelperMethodFrame_1OBJ: 0ffde65c] System.RuntimeMethodHandle._InvokeMethodFast(System.Object, System.Object[], System.SignatureStruct ByRef, System.Reflection.MethodAttributes, System.RuntimeTypeHandle)
0ffde6cc 792d52d8 System.RuntimeMethodHandle.InvokeMethodFast(System.Object, System.Object[], System.Signature, System.Reflection.MethodAttributes, System.RuntimeTypeHandle)
0ffde71c 792d5086 System.Reflection.RuntimeMethodInfo.Invoke(System.Object, System.Reflection.BindingFlags, System.Reflection.Binder, System.Object[], System.Globalization.CultureInfo, Boolean)
0ffde758 792d4f6e System.Reflection.RuntimeMethodInfo.Invoke(System.Object, System.Reflection.BindingFlags, System.Reflection.Binder, System.Object[], System.Globalization.CultureInfo)
0ffde778 65dd912b System.Web.Services.Protocols.LogicalMethodInfo.Invoke(System.Object, System.Object[])
0ffde79c 65e1094e System.Web.Services.Protocols.WebServiceHandler.Invoke()
0ffde7dc 65e10625 System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
0ffde80c 65e10fb7 System.Web.Services.Protocols.SyncSessionlessHandler.ProcessRequest(System.Web.HttpContext)
0ffde820 660ad8f6 System.Web.HttpApplication+CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
0ffde854 6608132c System.Web.HttpApplication.ExecuteStep(IExecutionStep, Boolean ByRef)
0ffde894 6608c3a3 System.Web.HttpApplication+ApplicationStepManager.ResumeSteps(System.Exception)
0ffde8e4 660808ac System.Web.HttpApplication.System.Web.IHttpAsyncHandler.BeginProcessRequest(System.Web.HttpContext, System.AsyncCallback, System.Object)
0ffde900 66083e1c System.Web.HttpRuntime.ProcessRequestInternal(System.Web.HttpWorkerRequest)
0ffde934 66083ac3 System.Web.HttpRuntime.ProcessRequestNoDemand(System.Web.HttpWorkerRequest)
0ffde944 66082c5c System.Web.Hosting.ISAPIRuntime.ProcessRequest(IntPtr, Int32)
0ffdeb58 79f68c4e [ContextTransitionFrame: 0ffdeb58] 
0ffdeb8c 79f68c4e [GCFrame: 0ffdeb8c] 
0ffdece8 79f68c4e [ComMethodFrame: 0ffdece8] 

我错过了什么?

故障转储显示引发的异常,但有一个 try-catch 应该捕获它

应重构代码并避免对控制流使用异常。您应该使用TryParse

public static int StringToInt(string numStr, int defValue)
{
    int num;
    if (!Int32.TryParse(numStr, out num)) { return defValue; }
    return num;
}

奇怪的是,异常没有被抓住,这绝对不应该发生。也许您的客户使用的产品版本与您拥有的源代码不同。