Skip to content

Commit f3acc19

Browse files
authored
Merge pull request #141 from jl0pd/exception-dispatch-info
Use ExceptionDispatchInfo
2 parents 365551a + 70923a7 commit f3acc19

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.Linq.Expressions;
1818
using System.Reflection;
1919
using System.Runtime.CompilerServices;
20+
using System.Runtime.ExceptionServices;
2021
using System.Runtime.InteropServices;
2122
using System.Text;
2223
using System.Text.RegularExpressions;
@@ -287,15 +288,17 @@ protected enum TryBlockEvaluatedState
287288
{ExpressionOperator.ConditionalAnd, (dynamic left, dynamic right) => {
288289
if ( left is BubbleExceptionContainer leftExceptionContainer)
289290
{
290-
throw leftExceptionContainer.Exception;
291+
leftExceptionContainer.Throw();
292+
return null; // this line is never reached
291293
}
292294
else if (!left)
293295
{
294296
return false;
295297
}
296298
else if (right is BubbleExceptionContainer rightExceptionContainer)
297299
{
298-
throw rightExceptionContainer.Exception;
300+
rightExceptionContainer.Throw();
301+
return null; // this line is never reached
299302
}
300303
else
301304
{
@@ -308,15 +311,17 @@ protected enum TryBlockEvaluatedState
308311
{ExpressionOperator.ConditionalOr, (dynamic left, dynamic right) => {
309312
if ( left is BubbleExceptionContainer leftExceptionContainer)
310313
{
311-
throw leftExceptionContainer.Exception;
314+
leftExceptionContainer.Throw();
315+
return null; // this line is never reached
312316
}
313317
else if (left)
314318
{
315319
return true;
316320
}
317321
else if (right is BubbleExceptionContainer rightExceptionContainer)
318322
{
319-
throw rightExceptionContainer.Exception;
323+
rightExceptionContainer.Throw();
324+
return null; // this line is never reached
320325
}
321326
else
322327
{
@@ -2147,20 +2152,15 @@ where method.GetParameters()[0].ParameterType == objType // static extMethod(thi
21472152
}
21482153
catch (NullReferenceException nullException)
21492154
{
2150-
stack.Push(new BubbleExceptionContainer()
2151-
{
2152-
Exception = nullException
2153-
});
2155+
stack.Push(new BubbleExceptionContainer(nullException));
21542156

21552157
return true;
21562158
}
21572159
catch (Exception ex)
21582160
{
21592161
//Transport the exception in stack.
2160-
stack.Push(new BubbleExceptionContainer()
2161-
{
2162-
Exception = new ExpressionEvaluatorSyntaxErrorException($"The call of the method \"{varFuncName}\" on type [{objType}] generate this error : {ex.InnerException?.Message ?? ex.Message}", ex)
2163-
});
2162+
var nestedException = new ExpressionEvaluatorSyntaxErrorException($"The call of the method \"{varFuncName}\" on type [{objType}] generate this error : {ex.InnerException?.Message ?? ex.Message}", ex);
2163+
stack.Push(new BubbleExceptionContainer(nestedException));
21642164
return true; //Signals an error to the parsing method array call
21652165
}
21662166
}
@@ -2410,10 +2410,8 @@ where method.GetParameters()[0].ParameterType == objType // static extMethod(thi
24102410
catch (Exception ex)
24112411
{
24122412
//Transport the exception in stack.
2413-
stack.Push(new BubbleExceptionContainer()
2414-
{
2415-
Exception = new ExpressionEvaluatorSyntaxErrorException($"[{objType}] object has no public Property or Member named \"{varFuncName}\".", ex)
2416-
});
2413+
var nestedException = new ExpressionEvaluatorSyntaxErrorException($"[{objType}] object has no public Property or Member named \"{varFuncName}\".", ex);
2414+
stack.Push(new BubbleExceptionContainer(nestedException));
24172415
i--;
24182416
return true; //Signals an error to the parsing method array call
24192417
}
@@ -3082,7 +3080,7 @@ protected virtual bool EvaluateString(string expression, Stack<object> stack, re
30823080
object obj = Evaluate(innerExp.ToString());
30833081

30843082
if (obj is BubbleExceptionContainer bubbleExceptionContainer)
3085-
throw bubbleExceptionContainer.Exception;
3083+
bubbleExceptionContainer.Throw();
30863084

30873085
resultString.Append(obj);
30883086
}
@@ -3181,7 +3179,7 @@ void EvaluateFirstNextUnaryOp(int j, ref int parentIndex)
31813179
}
31823180
else
31833181
{
3184-
list[i] = new BubbleExceptionContainer() { Exception = ex }; //Transport the processing error
3182+
list[i] = new BubbleExceptionContainer(ex); //Transport the processing error
31853183
}
31863184
}
31873185
list.RemoveAt(i - 1);
@@ -3216,7 +3214,7 @@ void EvaluateFirstPreviousUnaryOp(int j)
32163214
}
32173215
else
32183216
{
3219-
list[i] = new BubbleExceptionContainer() { Exception = ex }; //Transport the processing error
3217+
list[i] = new BubbleExceptionContainer(ex); //Transport the processing error
32203218
}
32213219
}
32223220
list.RemoveAt(i + 1);
@@ -3252,7 +3250,7 @@ void EvaluateFirstPreviousUnaryOp(int j)
32523250
}
32533251
else
32543252
{
3255-
list[i] = new BubbleExceptionContainer() { Exception = ex }; //Transport the processing error
3253+
list[i] = new BubbleExceptionContainer(ex); //Transport the processing error
32563254
}
32573255
}
32583256
list.RemoveAt(i + 1);
@@ -3277,15 +3275,15 @@ void EvaluateFirstPreviousUnaryOp(int j)
32773275
{
32783276
if (item is BubbleExceptionContainer bubbleExceptionContainer)
32793277
{
3280-
throw bubbleExceptionContainer.Exception; //Throw the first occuring error
3278+
bubbleExceptionContainer.Throw(); //Throw the first occuring error
32813279
}
32823280
}
32833281
throw new ExpressionEvaluatorSyntaxErrorException("Syntax error. Check that no operator is missing");
32843282
}
32853283
else if (evaluationStackCount == 1 && stack.Peek() is BubbleExceptionContainer bubbleExceptionContainer)
32863284
{
32873285
//We reached the top level of the evaluation. So we want to throw the resulting exception.
3288-
throw bubbleExceptionContainer.Exception;
3286+
bubbleExceptionContainer.Throw();
32893287
}
32903288

32913289
return stack.Pop();
@@ -3358,7 +3356,7 @@ protected virtual object ManageKindOfAssignation(string expression, ref int inde
33583356
}
33593357

33603358
if (result is BubbleExceptionContainer exceptionContainer)
3361-
throw exceptionContainer.Exception;
3359+
exceptionContainer.Throw();
33623360

33633361
if (stack != null)
33643362
{
@@ -4692,7 +4690,14 @@ public partial class MethodsGroupEncaps
46924690

46934691
public partial class BubbleExceptionContainer
46944692
{
4695-
public Exception Exception { get; set; }
4693+
public BubbleExceptionContainer(Exception exception)
4694+
{
4695+
_dispatchInfo = ExceptionDispatchInfo.Capture(exception);
4696+
}
4697+
4698+
private readonly ExceptionDispatchInfo _dispatchInfo;
4699+
4700+
public void Throw() => _dispatchInfo.Throw();
46964701
}
46974702

46984703
public partial class ExpressionEvaluatorSyntaxErrorException : Exception

0 commit comments

Comments
 (0)