Skip to content

Commit d32310c

Browse files
Merge pull request #272 from IntelliTect/Chapter11Regions
Add regions to chapter 11
2 parents e6cc7ef + be9072e commit d32310c

8 files changed

+72
-32
lines changed

src/Chapter11/Listing11.01.ThrowingAnException.cs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{
33
using System;
44

5+
#region INCLUDE
56
public sealed class TextNumberParser
67
{
78
public static int Parse(string textDigit)
@@ -10,34 +11,34 @@ public static int Parse(string textDigit)
1011
{ "zero", "one", "two", "three", "four",
1112
"five", "six", "seven", "eight", "nine" };
1213

13-
#if !PRECSHARP7
1414
int result = Array.IndexOf(
1515
digitTexts,
1616
// Leveraging C# 2.0’s null-coalescing operator
1717
(textDigit ??
1818
// Leveraging C# 7.0’s throw expression
1919
throw new ArgumentNullException(nameof(textDigit))
2020
).ToLower());
21-
22-
#else
23-
if(textDigit == null) throw new ArgumentNullException(nameof(textDigit))
24-
int result = Array.IndexOf(
25-
digitTexts, textDigit?.ToLower());
26-
#endif
21+
#region EXCLUDE
22+
// If running with pre C# 7.0, you will have to use this
23+
// if (textDigit == null) throw new ArgumentNullException(nameof(textDigit));
24+
// int result = Array.IndexOf(
25+
// digitTexts, textDigit?.ToLower());
26+
#endregion EXCLUDE
2727

2828
if (result < 0)
2929
{
30-
#if !PRECSHARP6
30+
// Leveraging C# 6.0's nameof operator
3131
throw new ArgumentException(
3232
"The argument did not represent a digit", nameof(textDigit));
33-
#else
34-
throw new ArgumentException(
35-
"The argument did not represent a digit",
36-
"textDigit");
37-
#endif
33+
#region EXCLUDE
34+
// If pre C# 6.0 will have to use this because the nameof operator doesn't exist yet
35+
// throw new ArgumentException(
36+
// "The argument did not represent a digit",
37+
// "textDigit");
38+
#endregion EXCLUDE
3839
}
39-
4040
return result;
4141
}
4242
}
43+
#endregion INCLUDE
4344
}

src/Chapter11/Listing11.02.CatchingDifferentExceptionTypes.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter11.Listing11_02
22
{
33
#pragma warning disable 0168 // Disabled warning for unused variables for elucidation
4-
using System;
54
using System.ComponentModel;
5+
#region INCLUDE
6+
using System;
67

78
public sealed class Program
89
{
910
public static void Main(string[] args)
1011
{
1112
try
1213
{
14+
#region EXCLUDE
1315
//throw new Win32Exception(42);
1416
// ...
1517
//TextNumberParser.Parse("negative forty-two");
16-
// ...
18+
#endregion EXCLUDE
1719
throw new InvalidOperationException(
1820
"Arbitrary exception");
1921
// ...
@@ -30,7 +32,13 @@ public static void Main(string[] args)
3032
}
3133
catch(InvalidOperationException exception)
3234
{
35+
bool exceptionHandled = false;
3336
// Handle InvalidOperationException
37+
// ...
38+
if(!exceptionHandled)
39+
{
40+
throw;
41+
}
3442
}
3543
catch(Exception exception)
3644
{
@@ -43,4 +51,5 @@ public static void Main(string[] args)
4351
}
4452
}
4553
}
54+
#endregion INCLUDE
4655
}

src/Chapter11/Listing11.03.UsingExceptionDispatchInfoToRethrowException.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter11.Listing11_03
22
{
3-
#pragma warning disable 0168 // Disabled warning for unused variables for elucidation
3+
#region INCLUDE
44
using System;
5-
using System.IO;
6-
using System.Linq;
7-
using System.Net;
85
using System.Runtime.ExceptionServices;
96
using System.Threading.Tasks;
10-
7+
using System.Net;
8+
using System.Linq;
9+
using System.IO;
10+
#region EXCLUDE
1111
public sealed class Program
1212
{
1313
public static void Main(string[] args)
@@ -19,6 +19,7 @@ public static void Main(string[] args)
1919
}
2020

2121
Console.Write(url);
22+
#endregion EXCLUDE
2223
Task task = WriteWebRequestSizeAsync(url);
2324
try
2425
{
@@ -30,10 +31,12 @@ public static void Main(string[] args)
3031
catch (AggregateException exception)
3132
{
3233
exception = exception.Flatten();
34+
#region HIGHLIGHT
3335
ExceptionDispatchInfo.Capture(
3436
exception.InnerException??exception).Throw();
37+
#endregion HIGHLIGHT
3538
}
36-
39+
#endregion INCLUDE
3740
}
3841

3942

src/Chapter11/Listing11.04.CreatingCustomException.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter11.Listing11_04
55
{
6-
6+
#region INCLUDE
77
class DatabaseException : Exception
88
{
99
public DatabaseException(
@@ -39,7 +39,7 @@ public DatabaseException(
3939
{
4040
// ...
4141
}
42-
42+
#region EXCLUDE
4343
// Used for deserialization of exceptions
4444
public DatabaseException(
4545
SerializationInfo serializationInfo,
@@ -48,7 +48,9 @@ public DatabaseException(
4848
{
4949
//...
5050
}
51+
#endregion EXCLUDE
5152
}
53+
#endregion INCLUDE
5254

5355
// Create mock versions of the database exception classes rather
5456
// than referencing the real libraries.

src/Chapter11/Listing11.05.DefiningASerializableException.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@
33

44
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter11.Listing11_05
55
{
6-
6+
#region INCLUDE
7+
// Supporting serialization via an attribute
8+
#region HIGHLIGHT
9+
[Serializable]
10+
#endregion HIGHLIGHT
711
class DatabaseException : Exception
812
{
13+
#region EXCLUDE
914
public DatabaseException(
1015
string? message,
1116
System.Data.SqlClient.SQLException? exception)
@@ -39,16 +44,20 @@ public DatabaseException(
3944
{
4045
// ...
4146
}
47+
#endregion EXCLUDE
4248

4349
// Used for deserialization of exceptions
4450
public DatabaseException(
51+
#region HIGHLIGHT
4552
SerializationInfo serializationInfo,
4653
StreamingContext context)
4754
: base(serializationInfo, context)
48-
{
49-
//...
50-
}
55+
#endregion HIGHLIGHT
56+
{
57+
//...
58+
}
5159
}
60+
#endregion INCLUDE
5261

5362
// Create mock versions of the database exception classes rather
5463
// than referencing the real libraries.
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter11.Listing11_06
22
{
3+
#region INCLUDE
4+
using System;
35
public class Program
46
{
57
public static void Main()
68
{
79
// int.MaxValue equals 2147483647
810
int n = int.MaxValue;
911
n = n + 1;
10-
System.Console.WriteLine(n);
12+
Console.WriteLine(n);
1113
}
1214
}
13-
15+
#endregion INCLUDE
1416
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter11.Listing11_07
22
{
3+
#region INCLUDE
4+
using System;
35
public class Program
46
{
57
public static void Main()
68
{
9+
#region HIGHLIGHT
710
checked
811
{
12+
#endregion HIGHLIGHT
913
// int.MaxValue equals 2147483647
1014
int n = int.MaxValue;
1115
n = n + 1;
12-
System.Console.WriteLine(n);
16+
Console.WriteLine(n);
17+
#region HIGHLIGHT
1318
}
19+
#endregion HIGHLIGHT
1420
}
1521
}
22+
#endregion INCLUDE
1623
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter11.Listing11_08
22
{
3+
#region INCLUDE
4+
using System;
35
public class Program
46
{
57
public static void Main()
68
{
9+
#region HIGHLIGHT
710
unchecked
811
{
12+
#endregion HIGHLIGHT
913
// int.MaxValue equals 2147483647
1014
int n = int.MaxValue;
1115
n = n + 1;
12-
System.Console.WriteLine(n);
16+
Console.WriteLine(n);
17+
#region HIGHLIGHT
1318
}
19+
#endregion HIGHLIGHT
1420
}
1521
}
22+
#endregion INCLUDE
1623
}

0 commit comments

Comments
 (0)