-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathAsyncSample.cs
More file actions
73 lines (65 loc) · 2.02 KB
/
AsyncSample.cs
File metadata and controls
73 lines (65 loc) · 2.02 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
62
63
64
65
66
67
68
69
70
71
72
73
#if Ver4_5 || (Ver4 && Plus)
using System;
using System.IO;
using System.Threading.Tasks;
namespace VersionSample.Csharp6
{
/// <summary>
/// catch 句、finally 句内での await は、
/// async/await さえ使えるバージョンであれば必ず使える。
/// </summary>
public class AsyncSample
{
public static async Task XAsync()
{
try
{
await Csharp5.AsyncSample.XAsync();
}
catch (InvalidOperationException e)
{
using (var s = new StreamWriter("error.txt"))
await s.WriteAsync(e.ToString());
}
finally
{
using (var s = new StreamWriter("trace.txt"))
await s.WriteAsync("XAsync done.");
}
}
public static async Task AproxSameXAsync()
{
// 要は、1段階多く try で囲って catch-throw し直ししたり、一度例外を変数で受けて、null じゃなかったら処理するみたいなことしてる。
Exception ex1 = null;
try
{
Exception ex2 = null;
try
{
await Csharp5.AsyncSample.XAsync();
}
catch (InvalidOperationException e)
{
ex2 = e;
}
if (ex2 != null)
{
using (var s = new StreamWriter("error.txt"))
await s.WriteAsync(ex2.ToString());
}
}
catch(Exception e1)
{
ex1 = e1;
}
using (var s = new StreamWriter("trace.txt"))
await s.WriteAsync("XAsync done.");
if (ex1 != null)
{
throw ex1;
// ほんとは、スタックトレースを保存する処理とかがさらに挟まってる
}
}
}
}
#endif