-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathAsynchronousExceptionsExamples.cs
More file actions
61 lines (55 loc) · 1.94 KB
/
Copy pathAsynchronousExceptionsExamples.cs
File metadata and controls
61 lines (55 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System;
using System.Threading.Tasks;
namespace CSharpCodeSamples
{
public class AsynchronousExceptionsExamples
{
public void ExceptionPriorToTaskCreation()
{
#pragma warning disable 168 // The variable 'var' is assigned but its value is never used
#region ExceptionPriorToTaskCreation
try
{
Task myTask = SomeOperationAsync();
}
catch (ArgumentException ex)
{
// ex was thrown directly by SomeOperationAsync. This cannot occur if SomeOperationAsync is an async
// function (§10.15 - C# Language Specification Version 5.0).
}
#endregion
#pragma warning restore 168
}
public void ExceptionDuringTaskExecution()
{
#region ExceptionDuringTaskExecution
try
{
Task myTask = SomeOperationAsync();
myTask.Wait();
}
catch (AggregateException wrapperEx)
{
ArgumentException ex = wrapperEx.InnerException as ArgumentException;
if (ex == null)
throw;
// ex was thrown during the asynchronous portion of SomeOperationAsync. This is always the case if
// SomeOperationAsync is an async function (§10.15 - C# Language Specification Version 5.0).
}
#endregion
}
public void AsynchronousMethodAsContinuation()
{
#region AsynchronousMethodAsContinuation
// original asynchronous method invocation
Task task1 = SomeOperationAsync();
// method invocation treated as a continuation
Task task2 = task1.ContinueWith(_ => SomeOperationAsync());
#endregion
}
private static Task SomeOperationAsync()
{
throw new NotSupportedException();
}
}
}