From 8c1a063a8b579e201e370c6c6e1bb70a3fd1f7d3 Mon Sep 17 00:00:00 2001
From: Sam Harwell <Sam.Harwell@microsoft.com>
Date: Thu, 16 Jan 2020 08:26:28 -0800
Subject: [PATCH] Check exception status even if TF_SessionRun throws an
 exception

---
 .../TensorflowUtils.cs                        | 24 +++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)

diff --git a/src/Microsoft.ML.TensorFlow/TensorflowUtils.cs b/src/Microsoft.ML.TensorFlow/TensorflowUtils.cs
index cae9f3b379..49cb34040e 100644
--- a/src/Microsoft.ML.TensorFlow/TensorflowUtils.cs
+++ b/src/Microsoft.ML.TensorFlow/TensorflowUtils.cs
@@ -3,7 +3,6 @@
 // See the LICENSE file in the project root for more information.
 
 using System;
-using System.Collections.Generic;
 using System.IO;
 using System.Linq;
 using System.Security.AccessControl;
@@ -488,9 +487,26 @@ public Tensor[] Run()
 
                 unsafe
                 {
-                    c_api.TF_SessionRun(_session, null, _inputs, _inputValues,
-                         _inputs.Length, _outputs, _outputValues, _outputValues.Length, _operations,
-                        _operations.Length, IntPtr.Zero, _status);
+                    try
+                    {
+                        c_api.TF_SessionRun(_session, null, _inputs, _inputValues,
+                             _inputs.Length, _outputs, _outputValues, _outputValues.Length, _operations,
+                            _operations.Length, IntPtr.Zero, _status);
+                    }
+                    catch (Exception ex)
+                    {
+                        try
+                        {
+                            _status.Check(throwException: true);
+                        }
+                        catch (Exception statusException)
+                        {
+                            throw new AggregateException(statusException, ex);
+                        }
+
+                        // _status didn't provide more information, so just rethrow the original exception
+                        throw;
+                    }
                 }
 
                 _status.Check(true);