Skip to content

Commit 3a2bccd

Browse files
committed
ProgramHost: Print exception when no UnhandledException handlers are set.
1 parent 24006b1 commit 3a2bccd

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/hosting/ProgramContext.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ internal ProgramContext(ReadOnlyMemory<string> arguments)
3939
// The following methods must never throw.
4040

4141
[SuppressMessage("", "CA1031")]
42-
internal void RaiseUnhandledException(Exception exception)
42+
internal bool RaiseUnhandledException(Exception exception)
4343
{
4444
var ev = UnhandledException;
4545

4646
if (ev == null)
47-
return;
47+
return false;
4848

4949
foreach (var dg in ev.GetInvocationList())
5050
{
@@ -56,6 +56,8 @@ internal void RaiseUnhandledException(Exception exception)
5656
{
5757
}
5858
}
59+
60+
return true;
5961
}
6062

6163
[SuppressMessage("", "CA1031")]

src/hosting/ProgramHost.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@ public static async Task RunAsync<TProgram>(ReadOnlyMemory<string> arguments)
3030

3131
domain.UnhandledException += (_, e) =>
3232
{
33-
context.RaiseUnhandledException((Exception)e.ExceptionObject);
33+
var ex = (Exception)e.ExceptionObject;
34+
35+
// The terminal might be in raw mode when this happens and checking IsRawMode is racey. To ensure sensible
36+
// output, just always use CRLF here.
37+
if (!context.RaiseUnhandledException(ex))
38+
Terminal.Error($"Unhandled exception. {ex.ToString().ReplaceLineEndings("\r\n")}\r\n");
3439

3540
// Most users expect ProcessExit to run on both normal and abnormal termination. This call makes that happen
3641
// and also ensures that the terminal cleanup we do in ProcessExit runs. One unfortunate downside of this

0 commit comments

Comments
 (0)