forked from dotnet/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
98 lines (85 loc) · 2.8 KB
/
Program.cs
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System;
using System.Runtime.CompilerServices;
using static NewStyle.ExceptionExtensions;
namespace NewInCSharp6
{
public class Program
{
public static void Main(string[] args)
{
var t = new OldStyle.Student();
t.Grades.Add(4.5);
var person = new NewStyle.Student("first", "last");
// <NullConditional>
var first = person?.FirstName;
// </NullConditional>
// <NullCoalescing>
first = person?.FirstName ?? "Unspecified";
// </NullCoalescing>
var test = new NewStyle.Student("first", "last");
test.Grades.Add(1.0);
test.Grades.Add(1.5);
test.Grades.Add(2.0);
test.Grades.Add(3.5);
test.Grades.Add(2.0);
test.Grades.Add(4.0);
test.Grades.Add(1.50);
test.Grades.Add(2.25);
test.Grades.Add(3.5);
test.Grades.Add(1.0);
test.Grades.Add(1.0);
Console.WriteLine(test.GetAllGrades());
}
// <LogException>
public void MethodThatFailsSometimes()
{
try {
PerformFailingOperation();
} catch (Exception e) when (e.LogException())
{
// This is never reached!
}
}
// </LogException>
// <LogExceptionRecovery>
public void MethodThatFailsButHasRecoveryPath()
{
try {
PerformFailingOperation();
} catch (Exception e) when (e.LogException())
{
// This is never reached!
}
catch (RecoverableException ex)
{
Console.WriteLine(ex.ToString());
// This can still catch the more specific
// exception because the exception filter
// above always returns false.
// Perform recovery here
}
}
// </LogExceptionRecovery>
// <LogExceptionDebugger>
public void MethodThatFailsWhenDebuggerIsNotAttached()
{
try {
PerformFailingOperation();
} catch (Exception e) when (e.LogException())
{
// This is never reached!
}
catch (RecoverableException ex) when (!System.Diagnostics.Debugger.IsAttached)
{
Console.WriteLine(ex.ToString());
// Only catch exceptions when a debugger is not attached.
// Otherwise, this should stop in the debugger.
}
}
// </LogExceptionDebugger>
private void PerformFailingOperation() {}
}
public class RecoverableException : Exception
{
}
}