Skip to content

Commit b099805

Browse files
ClémentClément
authored andcommitted
Some notes on exceptions.
1 parent 9eda0aa commit b099805

10 files changed

Lines changed: 439 additions & 457 deletions

File tree

source/code/projects/ExceptionParse/ExceptionParse/Program.cs

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,59 @@ class Program
55
static void Main()
66
{
77
string answer;
8-
int value = 0;
8+
int value = 0; // We *have* to give this a value.
99
bool valid = false;
10-
11-
Console.WriteLine("Test with" +
12-
"\n\t- nothing (ctrl + d on linux, ctrl + z on windows), " +
13-
"\n\t- \"No\"," +
14-
"\n\t- " + int.MaxValue + "+ 1 = 2147483648.");
10+
11+
Console.WriteLine(
12+
"Test with"
13+
+ "\n\t- nothing (ctrl + d on linux, ctrl + z on windows), "
14+
+ "\n\t- \"No\","
15+
+ "\n\t- "
16+
+ int.MaxValue
17+
+ "+ 1 = 2147483648."
18+
);
1519
// Illustrating the exceptions int.Parse can throw.
1620
answer = Console.ReadLine();
17-
18-
try
19-
{
20-
value = int.Parse(answer);
21+
22+
try
23+
{
24+
value = int.Parse(answer);
2125
valid = true;
22-
}
23-
catch (ArgumentNullException)
24-
{
25-
Console.WriteLine("No argument provided.");
26-
}
27-
catch (FormatException)
28-
{
29-
Console.WriteLine("The string does not contain only number characters.");
30-
}
31-
catch (OverflowException)
32-
{
33-
Console.WriteLine("The number is greater than what an integer can store.");
34-
}
35-
finally{
36-
Console.WriteLine("You entered \"" + answer + "\".");
26+
}
27+
catch (ArgumentNullException)
28+
{
29+
Console.WriteLine("No argument provided.");
30+
}
31+
catch (FormatException)
32+
{
33+
Console.WriteLine(
34+
"The string does not contain only number characters."
35+
);
36+
}
37+
catch (OverflowException)
38+
{
39+
Console.WriteLine(
40+
"The number is greater than what an integer can store."
41+
);
42+
}
43+
finally
44+
{
45+
Console.WriteLine("You entered \"" + answer + "\".");
3746

38-
if(valid){
39-
Console.WriteLine("I can convert this to the following numerical value:" + value);
40-
}
41-
else{
42-
Console.WriteLine("I cannot convert this to a numerical value.");
43-
}
44-
}
47+
if (valid)
48+
{
49+
Console.WriteLine(
50+
"I can convert this to the following numerical value:"
51+
+ value
52+
);
53+
}
54+
else
55+
{
56+
Console.WriteLine(
57+
"I cannot convert this to a numerical value."
58+
);
59+
Console.WriteLine(value);
60+
}
61+
}
4562
}
4663
}

source/code/projects/PQueue_heap/PQueue/Program.cs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,23 @@ static void Main(string[] args)
4747
myQueue.Add(1, "Cardiac arrest");
4848
Console.WriteLine(myQueue);
4949

50-
PQueue<int, string> heapProblem = new PQueue<int, string>(6);
51-
heapProblem.Add(10, null);
52-
Console.WriteLine(heapProblem);
53-
heapProblem.Add(2, null);
54-
Console.WriteLine(heapProblem);
55-
heapProblem.Add(5, null);
56-
Console.WriteLine(heapProblem);
57-
heapProblem.Add(7, null);
58-
Console.WriteLine(heapProblem);
59-
heapProblem.Extract();
60-
Console.WriteLine(heapProblem);
61-
heapProblem.Add(3, null);
62-
Console.WriteLine(heapProblem);
63-
heapProblem.Add(12, null);
64-
Console.WriteLine(heapProblem);
65-
66-
67-
}
50+
PQueue<int, string> heapProblem = new PQueue<
51+
int,
52+
string
53+
>(6);
54+
heapProblem.Add(10, null);
55+
Console.WriteLine(heapProblem);
56+
heapProblem.Add(2, null);
57+
Console.WriteLine(heapProblem);
58+
heapProblem.Add(5, null);
59+
Console.WriteLine(heapProblem);
60+
heapProblem.Add(7, null);
61+
Console.WriteLine(heapProblem);
62+
heapProblem.Extract();
63+
Console.WriteLine(heapProblem);
64+
heapProblem.Add(3, null);
65+
Console.WriteLine(heapProblem);
66+
heapProblem.Add(12, null);
67+
Console.WriteLine(heapProblem);
68+
}
6869
}

source/code/projects/SDictionary/SDictionary/Program.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
class Program
44
{
5-
static void Main(string[] args)
6-
{
7-
SDictionary friends = new SDictionary(11);
8-
friends.Add("Bob", null);
9-
friends.Add("Pete", null);
10-
friends.Add("Mary", null);
11-
friends.Add("Lora", null);
12-
}
5+
static void Main(string[] args)
6+
{
7+
SDictionary friends = new SDictionary(11);
8+
friends.Add("Bob", null);
9+
friends.Add("Pete", null);
10+
friends.Add("Mary", null);
11+
friends.Add("Lora", null);
12+
}
1313
}

source/code/projects/SDictionary/SDictionary/SDictionary.cs

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ private class Cell
77
public string Value { get; set; }
88
public string Key { get; set; }
99

10-
public Cell(
11-
string keyP,
12-
string valueP
13-
)
10+
public Cell(string keyP, string valueP)
1411
{
1512
Key = keyP;
1613
Value = valueP;
@@ -24,31 +21,25 @@ public override string ToString()
2421

2522
private Cell[] table;
2623

27-
public SDictionary(
28-
int size = 31
29-
)
24+
public SDictionary(int size = 31)
3025
{
3126
table = new Cell[size];
3227
}
3328

34-
public int GetIndex(string keyP, int countP)
35-
{
36-
return ((int)(keyP[0]) + countP) % table.Length;
37-
}
38-
public void Add(string keyP, string valueP)
29+
public int GetIndex(string keyP, int countP)
30+
{
31+
return ((int)(keyP[0]) + countP) % table.Length;
32+
}
33+
34+
public void Add(string keyP, string valueP)
35+
{
36+
int count = 0;
37+
int index = GetIndex(keyP, count);
38+
while (table[index] != null)
3939
{
40-
int count = 0;
41-
int index = GetIndex(keyP, count);
42-
while (
43-
table[index] != null
44-
)
45-
{
46-
count++;
47-
index = GetIndex(keyP, count);
48-
}
49-
table[index] = new Cell(
50-
keyP,
51-
valueP
52-
);
40+
count++;
41+
index = GetIndex(keyP, count);
5342
}
43+
table[index] = new Cell(keyP, valueP);
44+
}
5445
}

source/code/projects/SDictionary_solution/SDictionary/Program.cs

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,42 @@
22

33
class Program
44
{
5-
static void Main(string[] args)
6-
{
7-
SDictionary friends = new SDictionary(11);
8-
friends.Add("Bob", null);
9-
friends.Add("Pete", null);
10-
friends.Add("Mary", null);
11-
friends.Add("Lora", null);
12-
Console.WriteLine(friends);
13-
// Inserting "Lora" again.
14-
friends.Add("Lora", null);
15-
Console.WriteLine(friends);
5+
static void Main(string[] args)
6+
{
7+
SDictionary friends = new SDictionary(11);
8+
friends.Add("Bob", null);
9+
friends.Add("Pete", null);
10+
friends.Add("Mary", null);
11+
friends.Add("Lora", null);
12+
Console.WriteLine(friends);
13+
// Inserting "Lora" again.
14+
friends.Add("Lora", null);
15+
Console.WriteLine(friends);
1616

17-
friends.Add("Bob", null);
18-
friends.Add("Bob", null);
19-
friends.Add("Bob", null);
20-
friends.Add("Bob", null);
21-
friends.Add("Bob", null);
22-
friends.Add("Bob", null);
23-
// At this point the dictionary is full:
24-
Console.WriteLine(friends);
25-
// Uncomment the following to see the program
26-
// enters an infinite loop.
27-
// friends.Add("Bob", null);
17+
friends.Add("Bob", null);
18+
friends.Add("Bob", null);
19+
friends.Add("Bob", null);
20+
friends.Add("Bob", null);
21+
friends.Add("Bob", null);
22+
friends.Add("Bob", null);
23+
// At this point the dictionary is full:
24+
Console.WriteLine(friends);
25+
// Uncomment the following to see the program
26+
// enters an infinite loop.
27+
// friends.Add("Bob", null);
2828

29-
// Exhibiting Delete incorrect behavior:
30-
SDictionary demo = new SDictionary(2);
31-
string error = "Alex";
32-
demo.Add("Alice", null);
33-
demo.Add(error, null);
34-
demo.Delete("Alice");
35-
Console.WriteLine($"{error} is in dictionary: {demo.Delete(error)}.");
36-
// Done: the program will believe that the
37-
// string error ("Alex") is not
38-
// in the demo array, while it is.
39-
Console.WriteLine(demo);
40-
41-
}
29+
// Exhibiting Delete incorrect behavior:
30+
SDictionary demo = new SDictionary(2);
31+
string error = "Alex";
32+
demo.Add("Alice", null);
33+
demo.Add(error, null);
34+
demo.Delete("Alice");
35+
Console.WriteLine(
36+
$"{error} is in dictionary: {demo.Delete(error)}."
37+
);
38+
// Done: the program will believe that the
39+
// string error ("Alex") is not
40+
// in the demo array, while it is.
41+
Console.WriteLine(demo);
42+
}
4243
}

0 commit comments

Comments
 (0)