我对visual studio有一个小问题.
我有一个抛出CustomException的方法
我有一个抛出CustomException的方法
如果我在try / catch中包装调用此方法的代码,我可以在调试器中看到异常详细信息
如果我删除try / catch我可以看到“errors”属性有Count = 4但是我看不到错误…
这是预期的还是一个错误?
我正在使用vs2015 enterprise和.NET 4.5.2
您可以轻松地重现它:
1)用这个创建一个类库
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace ClassLibrary1
- {
- public static class Class1
- {
- public static void DoSomethingThatThrowsException()
- {
- throw new MyException(Enumerable.Range(1,4).Select(e => new MyError() { Message = "error " + e.ToString() }).ToList());
- }
- }
- public class MyException : Exception
- {
- public IEnumerable<MyError> errors { get; set; }
- public MyException(IEnumerable<MyError> theErrors) { errors = theErrors; }
- }
- public class MyError { public string Message { get; set; } }
- }
2)创建一个控制台应用程序:
- using ClassLibrary1;
- namespace ConsoleApplicationException
- {
- class Program
- {
- static void Main(string[] args)
- {
- try
- {
- Class1.DoSomethingThatThrowsException();
- }
- catch (MyException ex)
- {
- //Here I can expand ex.errors;
- }
- //Here I can see that Count=4 but I cannot see the errors...
- Class1.DoSomethingThatThrowsException();
- }
- }
- }
PS
我可以使用“DebuggerDisplay”属性解决我的问题,我只是想知道为什么Visual Studio不能按预期工作
- [DebuggerDisplay("FullDetails = {FullDetails}")]
- public class MyException : Exception
- {
- public IEnumerable<MyError> errors { get; set; }
- public MyException(IEnumerable<MyError> theErrors) { errors = theErrors; }
- public string FullDetails { get { return string.Join(",",errors.Select(e => e.Message)); } }
- }
更新
如果我将List更改为Array,我有同样的问题,但如果我将其更改为Dictionary,我可以看到第一条记录!