Skip to content

Commit 60a1e58

Browse files
committed
Add new part solver wrapper that improves exception handling with a debugger
The new wrapper will catch any exception throw by the solver and breaks the debugger if attached, otherwise the wrapper method re-throws to crash the program like normal.
1 parent 50cfad7 commit 60a1e58

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

AdventOfCode/Solutions/ASolution.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Diagnostics;
23
using System.IO;
34
using System.Net;
45
using System.Net.Http;
@@ -24,9 +25,9 @@ private protected ASolution(int day, int year, string title)
2425
Day = day;
2526
Year = year;
2627
Title = title;
27-
_input = new Lazy<string>(() => LoadInput());
28-
_part1 = new Lazy<string>(() => SolvePartOne());
29-
_part2 = new Lazy<string>(() => SolvePartTwo());
28+
_input = new Lazy<string>(LoadInput);
29+
_part1 = new Lazy<string>(() => SolveSafely(SolvePartOne));
30+
_part2 = new Lazy<string>(() => SolveSafely(SolvePartTwo));
3031
}
3132

3233
public void Solve(int part = 0)
@@ -118,6 +119,25 @@ string LoadInput()
118119
return input;
119120
}
120121

122+
private string SolveSafely(Func<string> solver)
123+
{
124+
try
125+
{
126+
return solver();
127+
}
128+
catch( Exception ) {
129+
if( Debugger.IsAttached )
130+
{
131+
Debugger.Break();
132+
return string.Empty;
133+
}
134+
else
135+
{
136+
throw;
137+
}
138+
}
139+
}
140+
121141
protected abstract string SolvePartOne();
122142
protected abstract string SolvePartTwo();
123143
}

0 commit comments

Comments
 (0)