當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。