c# - 有没有办法为任何 System.IndexOutOfRangeException 消息创建通用 EventHandler?
问题描述
对于一般的任何 UnhandledException 消息,我可以像这样创建一个新的 EventHandler:
程序.cs:
static void Main()
{
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException)
}
表格.cs:
Application.ThreadException += unhandledERROR;
void unhandledERROR(object sender, System.Threading.ThreadExceptionEventArgs e)
{
Console.WriteLine("ERROR Unhandled Exception");
}
当出现 System.IndexOutOfRangeException 消息时,还有一种方法可以设置通用 EventHandler 吗?
解决方案
您的代码不应包含任何IndexOutOfRangeException
异常。真的不应该有未处理 IndexOutOfRangeException
的异常。
正确的处理方式IndexOutOfRangeException
是用try
和catch
在可能遇到的方法中。
try
{
// Code that can throw IndexOutOfRangeException
}
catch(IndexOutOfRangeException ex)
{
// Log, handle, etc.
}
在最后的处理程序中捕获异常为时已晚。
推荐阅读
- kendo-ui - Kendo:当我保存和恢复网格状态时如何保留工具栏?
- unity3d - MRTK 中的哪个脚本增强了全息图稳定性?
- javascript - 调用类型为 submit 的按钮不会触发验证
- bash - 在 Bash 中修改/更改时间
- javascript - 如何从用户内存上传并在地图上显示 kml 文件
- react-final-form - 如何以 react-final-form 转换“values”对象?
- kubernetes - 如何让 Kerberos 身份验证在 k8s 中工作
- angular - 带有 Angular 和响应式 Spring Security 的 OAuth2
- jquery - 点击toggle打开所有的div,如何避免呢?
- javascript - 如何将一个对象映射到另一个对象?