在Application.Run()发生异常时有什么选择?

时间:2013-05-16 11:28:58

标签: c# winforms exception exception-handling

我正在使用名为GreatMaps.NET精彩库在我​​的应用程序中进行一些映射。我真的很喜欢这个库(并且强烈推荐它),但我偶尔会从一个内部的GreatMap.NET例程中获取一个未处理的ArgumentOutOfRangeException,它会一直突破到我的Program.Main()。由于我的应用程序没有机会捕获此错误并处理它,我是否应该接受此错误存在?或者,我有办法阻止这个问题吗?

我已经包含了调用堆栈,以防它显示我可能遗漏的内容。

System.ArgumentOutOfRangeException was unhandled
  Message=Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
  Source=mscorlib
  ParamName=index
  StackTrace:
       at System.ThrowHelper.ThrowArgumentOutOfRangeException()
       at System.Collections.Generic.List`1.get_Item(Int32 index)
       at GMap.NET.WindowsForms.GMapOverlay.DrawRoutes(Graphics g)
       at GMap.NET.WindowsForms.GMapOverlay.Render(Graphics g)
       at GMap.NET.WindowsForms.GMapControl.OnPaintOverlays(Graphics g)
       at GMap.NET.WindowsForms.GMapControl.OnPaint(PaintEventArgs e)
       at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
       at System.Windows.Forms.Control.WmPaint(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at System.Windows.Forms.UserControl.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at Spectrum.Foxhunt.Client.Program.Main(String[] args) in C:\Users\Michael\Documents\Visual Studio 2010\Projects\Spectrum\Spectrum.Foxhunt.Client\Program.cs:line 23
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
       at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
       at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
       at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
       at System.Activator.CreateInstance(ActivationContext activationContext)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

2 个答案:

答案 0 :(得分:3)

如果这肯定是该库中的一个错误,一个解决方案是创建一个派生自GMapControl的类,并使用该类而不是原始类。

在派生类中,您将覆盖OnPaint并捕获该异常:

public class GMapControlFixed : GMapControl
{
    public override void OnPaint(PaintEventArgs e)
    {
        try
        {
            base.OnPaint(e);
        }
        catch(ArgumentOutOfRangeException)
        {
            // discard - it's a bug in the original control.
        }
    }
}

答案 1 :(得分:1)

您可以尝试此操作来捕获任何未处理的异常,并在static void Main()

中使用它
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

事件处理程序:

private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) {
    //...
}

我用它来测试,找到奇怪的错误。