当前位置: 首页>>代码示例>>C#>>正文


C# Exception.Select方法代码示例

本文整理汇总了C#中System.Exception.Select方法的典型用法代码示例。如果您正苦于以下问题:C# Exception.Select方法的具体用法?C# Exception.Select怎么用?C# Exception.Select使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Exception的用法示例。


在下文中一共展示了Exception.Select方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: RunFromResult

        public static void RunFromResult()
        {
            // Test FromResult with value type
            {
                var results = new[] { -1, 0, 1, 1, 42, Int32.MaxValue, Int32.MinValue, 42, -42 }; // includes duplicate values to ensure that tasks from these aren't the same object
                Task<int>[] tasks = new Task<int>[results.Length];
                for (int i = 0; i < results.Length; i++)
                    tasks[i] = Task.FromResult(results[i]);

                // Make sure they've all completed
                for (int i = 0; i < tasks.Length; i++)
                    Assert.True(tasks[i].IsCompleted, "TaskRtTests.RunFromResult:    > FAILED: Task " + i + " should have already completed (value)");

                // Make sure they all completed successfully
                for (int i = 0; i < tasks.Length; i++)
                    Assert.True(tasks[i].Status == TaskStatus.RanToCompletion, "TaskRtTests.RunFromResult:    > FAILED: Task " + i + " should have already completed successfully (value)");

                // Make sure no two are the same instance
                for (int i = 0; i < tasks.Length; i++)
                {
                    for (int j = i + 1; j < tasks.Length; j++)
                    {
                        Assert.False(tasks[i] == tasks[j], "TaskRtTests.RunFromResult:    > FAILED: " + i + " and " + j + " created tasks should not be equal (value)");
                    }
                }

                // Make sure they all have the correct results
                for (int i = 0; i < tasks.Length; i++)
                    Assert.True(tasks[i].Result == results[i], "TaskRtTests.RunFromResult:    > FAILED: Task " + i + " had the result " + tasks[i].Result + " but should have had " + results[i] + " (value)");
            }

            // Test FromResult with reference type
            {
                var results = new[] { new object(), null, new object(), null, new object() }; // includes duplicate values to ensure that tasks from these aren't the same object
                Task<Object>[] tasks = new Task<Object>[results.Length];
                for (int i = 0; i < results.Length; i++)
                    tasks[i] = Task.FromResult(results[i]);

                // Make sure they've all completed
                for (int i = 0; i < tasks.Length; i++)
                    Assert.True(tasks[i].IsCompleted, "TaskRtTests.RunFromResult:    > FAILED: Task " + i + " should have already completed  (ref)");

                // Make sure they all completed successfully
                for (int i = 0; i < tasks.Length; i++)
                    Assert.True(tasks[i].Status == TaskStatus.RanToCompletion, "TaskRtTests.RunFromResult:    > FAILED: Task " + i + " should have already completed successfully (ref)");

                // Make sure no two are the same instance
                for (int i = 0; i < tasks.Length; i++)
                {
                    for (int j = i + 1; j < tasks.Length; j++)
                    {
                        Assert.False(tasks[i] == tasks[j], "TaskRtTests.RunFromResult:    > FAILED: " + i + " and " + j + " created tasks should not be equal (ref)");
                    }
                }

                // Make sure they all have the correct results
                for (int i = 0; i < tasks.Length; i++)
                    Assert.True(tasks[i].Result == results[i], "TaskRtTests.RunFromResult:    > FAILED: Task " + i + " had the wrong result (ref)");
            }

            // Test FromException
            {
                var exceptions = new Exception[] { new InvalidOperationException(), new OperationCanceledException(), new Exception(), new Exception() }; // includes duplicate values to ensure that tasks from these aren't the same object
                var tasks = exceptions.Select(e => Task.FromException<int>(e)).ToArray();

                // Make sure they've all completed
                for (int i = 0; i < tasks.Length; i++)
                    Assert.True(tasks[i].IsCompleted, "Task " + i + " should have already completed");

                // Make sure they all completed with an error
                for (int i = 0; i < tasks.Length; i++)
                    Assert.True(tasks[i].Status == TaskStatus.Faulted, "    > FAILED: Task " + i + " should have already faulted");

                // Make sure no two are the same instance
                for (int i = 0; i < tasks.Length; i++)
                {
                    for (int j = i + 1; j < tasks.Length; j++)
                    {
                        Assert.True(tasks[i] != tasks[j], "    > FAILED: " + i + " and " + j + " created tasks should not be equal");
                    }
                }

                // Make sure they all have the correct exceptions
                for (int i = 0; i < tasks.Length; i++)
                {
                    Assert.NotNull(tasks[i].Exception);
                    Assert.Equal(1, tasks[i].Exception.InnerExceptions.Count);
                    Assert.Equal(exceptions[i], tasks[i].Exception.InnerException);
                }

                // Make sure we handle invalid exceptions correctly
                Assert.Throws<ArgumentNullException>(() => { Task.FromException<int>(null); });

                // Make sure we throw from waiting on a faulted task
                Assert.Throws<AggregateException>(() => { var result = Task.FromException<object>(new InvalidOperationException()).Result; });

                // Make sure faulted tasks are actually faulted.  We have little choice for this test but to use reflection,
                // as the harness will crash by throwing from the unobserved event if a task goes unhandled (everywhere
                // other than here it's a bad thing for an exception to go unobserved)
                var faultedTask = Task.FromException<object>(new InvalidOperationException("uh oh"));
//.........这里部分代码省略.........
开发者ID:ChuangYang,项目名称:corefx,代码行数:101,代码来源:TaskRtTests.cs

示例2: FailureInfo

 internal FailureInfo(string rpFilename, Exception[] rpExceptions)
 {
     Filename = rpFilename;
     ExceptionContent = rpExceptions.Select(r => r.ToString()).Join(Environment.NewLine);
 }
开发者ID:amatukaze,项目名称:IntelligentNavalGun-Fx4,代码行数:5,代码来源:FailureInfo.cs


注:本文中的System.Exception.Select方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。