Skip to content

Commit 334c82d

Browse files
authored
Merge pull request #6 from taikodragon/solve-safely
Add PartSolver Wrapper to improve exception debugging
2 parents 50cfad7 + 11b7446 commit 334c82d

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-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
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Template project for solving Advent of Code in C#, running on [.NET 5.0](https:/
1111
- [Generating Previous Year's Solution Files](#generating-previous-years-solution-files)
1212
- [Using a Solution's Constructor](#using-a-solutions-constructor)
1313
- [Using .NET Core](#using-net-core)
14+
- [Automatic Debugger Break On Exception](#automatic-debugger-break-on-exception)
1415
- [Contributing](#contributing)
1516
- [License](#license)
1617

@@ -107,6 +108,8 @@ Simply swap out the target framework in `AdventOfCode.csproj`.
107108
+ <TargetFramework>netcoreapp3.1</TargetFramework>
108109
```
109110

111+
### Automatic Debugger Break On Exception
112+
When running your Solutions with a Debugger attached e.g. [VSCode](https://code.visualstudio.com/docs/editor/debugging) or [Visual Studio](https://docs.microsoft.com/en-us/visualstudio/debugger/quickstart-debug-with-managed?view=vs-2019) the ASolution base class will try to pause/break the debugger when there is an uncaught Exception thrown by your solution part. This allows for inspection with the debugger without having to specifically set-up additional exception handling within the debugger or your solution.
110113

111114
## Contributing
112115
Sure! Fork the project, make your changes, and create a pull request. Submitted issues and pull requests are quite welcome.

0 commit comments

Comments
 (0)