c# - 当 Application.Run() 发生异常时,您有什么选择?

标签 c# winforms exception

我正在使用一个名为 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: 

最佳答案

如果这确实是该库中的错误,一种解决方案是创建一个派生自 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.
        }
    }
}

关于c# - 当 Application.Run() 发生异常时,您有什么选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16586245/

相关文章:

c# - 计算剩余时间

c# - 使用 backgroundWorker 下载文件

vb.net - 你能创建一个没有定时器组件的定时器吗

exception - ASP.NET MVC : “An internal error occurred.” when loading certificate bytes with X509Certificate2

java - 如何计算每个方法可能抛出的异常集,包括运行时异常?

c# - VS 2008 Windows 服务安装程序不工作

c# - LINQ:返回列表中的非重复项

c# - 在运行时定位 Windows 窗体上的 ErrorProvider

java - 尝试处理 Spring Boot 应用程序中的异常时日志中出现 ErrorPageFilter 错误

c# - 使用设置和部署以及安装程序类卸载用 c# 编写的应用程序