列表排序中未处理的异常(Unhandled Exception in List Sort)

编程入门 行业动态 更新时间:2024-10-24 12:29:55
列表排序中未处理的异常(Unhandled Exception in List Sort)

所以,我有一个包含自定义类MyClass的列表 MyClass的属性可以为null(但不是本意)。 当这个类被排序时,使用自定义的排序器,在排序器访问这个空属性并引发异常的情况下,即使在排序方法周围有一个try-catch块,该异常也被认为是未处理的。 由于某种原因,异常仍然被写入控制台,这是异常处理程序正在执行的操作。 我有一个真正的应用程序与此相同的问题,导致我的单元测试失败,即使异常处理正确,我无法解释这一点。

所以我附上了一些示例代码来更好地解释自己,从VS运行它。 更新的代码 结果: System.InvalidOperationException 无法比较数组中的两个元素。 完成! 所以它似乎在处理我的自定义异常,并抛出它自己的?

using System; using System.Collections.Generic; using System.Data; namespace TestSortException { class Program { static void Main() { try { var list = new List<MyClass> { new MyClass("1"), new MyClass(null), new MyClass("fdsfsdf") }; list.Sort(new MyClassSorter()); } catch(Exception e) { Console.WriteLine(e.GetType()); Console.WriteLine(e.Message); } Console.WriteLine("Done!"); Console.ReadLine(); } } class MyClassSorter : IComparer<MyClass> { public int Compare(MyClass x, MyClass y) { // try // { if (x.MyString == y.MyString) return 0; // Unhandled??? Exception here if (x.MyString.Length > y.MyString.Length) return 1; return -1; // } // catch (Exception) // { // return -1; // } } } class MyClass { private string _myString; public string MyString { get { if (_myString == null) throw new DataException("MyString is Null"); return _myString; } } public MyClass(string myString) { _myString = myString; } } }

So, I have a list containing a custom class, MyClass MyClass has properties, which can be null (but aren't meant to be). When this class is sorted, using a custom sorter, where the sorter accesses this null property and throws an exception, the exception is considered unhandled, even though there is a try-catch block around the sort method. Now for some reason the exception still gets written to the console, which is what the exception handler is doing. I have a real application with this same issue, causing my unit tests to fail, even though the exception is handled correctly and I cannot explain this.

So I have attached some sample code to explain myself better, run this from VS. Updated Code Results: System.InvalidOperationException Failed to compare two elements in the array. Done! So it seems to be handling my custom exception, and throwing its own?

using System; using System.Collections.Generic; using System.Data; namespace TestSortException { class Program { static void Main() { try { var list = new List<MyClass> { new MyClass("1"), new MyClass(null), new MyClass("fdsfsdf") }; list.Sort(new MyClassSorter()); } catch(Exception e) { Console.WriteLine(e.GetType()); Console.WriteLine(e.Message); } Console.WriteLine("Done!"); Console.ReadLine(); } } class MyClassSorter : IComparer<MyClass> { public int Compare(MyClass x, MyClass y) { // try // { if (x.MyString == y.MyString) return 0; // Unhandled??? Exception here if (x.MyString.Length > y.MyString.Length) return 1; return -1; // } // catch (Exception) // { // return -1; // } } } class MyClass { private string _myString; public string MyString { get { if (_myString == null) throw new DataException("MyString is Null"); return _myString; } } public MyClass(string myString) { _myString = myString; } } }

最满意答案

在Sort方法中有一个try / catch块,是的 - 那个catch块捕获异常。 换句话说, Sort抛出一个异常,你的catch块捕获它。 它不会传播到Main之外 - 所以“完成!” 被打印。

这正是我所期望的。 您的体验以何种方式“未处理”? 你期望Sort不抛出异常吗? 它需要做一些事情来表明未能比较两个要素,这似乎是最合适的行动方式。

您的单元测试失败的方式是什么? 你有意给他们提供无效数据吗? 您希望您的比较代码如何对无效数据作出反应? 如果它应该忽略它(并返回基于另一个属性的比较),那么你应该主动检查属性而不是让异常传播。 在大多数情况下,我宁愿允许这个异常,如果这表明早期有bug的话。

编辑:根据你的其他评论,这听起来像你正在做适当的事情,让异常冒泡 - 但不清楚你看到异常不被处理的方式。

如果你在调试器中运行,它可能会破坏抛出的异常,但这并不意味着它不会被处理。 尝试更改异常设置或在没有调试器的情况下运行。

编辑:是的, Sort将捕获异常,而不是抛出一个InvalidOperationException - 但您可以使用该InnerException属性来获取原始的异常。 不幸的是文档没有指定这:(

There's a try/catch block round the Sort method, yes - and that catch block catches the exception. In other words, Sort throws an exception and your catch block catches it. It doesn't propagate out beyond Main - so "Done!" is printed.

This is exactly what I'd expect. In what way is it "unhandled" in your experience? Were you expecting Sort not to throw the exception? It needs to do something to indicate the failure to compare two elements, and this seems to be the most appropriate course of action.

In what way are your unit tests failing? Are you deliberately giving them invalid data? How do you want your comparison code to react to invalid data? If it should ignore it (and return a comparison based on another property), then you should actively check the property rather than letting an exception propagate. In most cases I'd rather allow the exception if this indicates that there's a bug earlier on though.

EDIT: Based on your other comments, it sounds like you're doing the appropriate thing, letting the exception bubble up - but it's not clear in what way you're seeing the exception not be handled.

If you're running in the debugger, it may be breaking on the exception being thrown, but that doesn't mean it won't be handled. Try either changing your exception settings or running without the debugger.

EDIT: Yes, Sort will catch the exception and throw an InvalidOperationException instead - but you can use the InnerException property of that exception to get hold of the original one. It's unfortunate that the documentation doesn't specify this :(

更多推荐

本文发布于:2023-08-07 07:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1462952.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中未   异常   列表   Unhandled   List

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!