11/******************************************************************************************************
22 Title : ExpressionEvaluator (https://github.com/codingseb/ExpressionEvaluator)
3- Version : 1.4.37 .0
3+ Version : 1.4.38 .0
44 (if last digit (the forth) is not a zero, the version is an intermediate version and can be unstable)
55
66 Author : Coding Seb
1616using System . Linq ;
1717using System . Reflection ;
1818using System . Runtime . CompilerServices ;
19+ using System . Runtime . ExceptionServices ;
1920using System . Runtime . InteropServices ;
2021using System . Text ;
2122using System . Text . RegularExpressions ;
@@ -286,6 +287,8 @@ protected enum TryBlockEvaluatedState
286287 { ExpressionOperator . ConditionalAnd , ( dynamic left , dynamic right ) => {
287288 if ( left is BubbleExceptionContainer leftExceptionContainer )
288289 {
290+ ExceptionDispatchInfo . Capture ( leftExceptionContainer . Exception ) . Throw ( ) ;
291+ // Will not go here but Resharper detect some errors because he does not know ExceptionDispatchInfo stuff.
289292 throw leftExceptionContainer . Exception ;
290293 }
291294 else if ( ! left )
@@ -294,6 +297,8 @@ protected enum TryBlockEvaluatedState
294297 }
295298 else if ( right is BubbleExceptionContainer rightExceptionContainer )
296299 {
300+ ExceptionDispatchInfo . Capture ( rightExceptionContainer . Exception ) . Throw ( ) ;
301+ // Will not go here but Resharper detect some errors because he does not know ExceptionDispatchInfo stuff.
297302 throw rightExceptionContainer . Exception ;
298303 }
299304 else
@@ -307,6 +312,8 @@ protected enum TryBlockEvaluatedState
307312 { ExpressionOperator . ConditionalOr , ( dynamic left , dynamic right ) => {
308313 if ( left is BubbleExceptionContainer leftExceptionContainer )
309314 {
315+ ExceptionDispatchInfo . Capture ( leftExceptionContainer . Exception ) . Throw ( ) ;
316+ // Will not go here but Resharper detect some errors because he does not know ExceptionDispatchInfo stuff.
310317 throw leftExceptionContainer . Exception ;
311318 }
312319 else if ( left )
@@ -315,6 +322,8 @@ protected enum TryBlockEvaluatedState
315322 }
316323 else if ( right is BubbleExceptionContainer rightExceptionContainer )
317324 {
325+ ExceptionDispatchInfo . Capture ( rightExceptionContainer . Exception ) . Throw ( ) ;
326+ // Will not go here but Resharper detect some errors because he does not know ExceptionDispatchInfo stuff.
318327 throw rightExceptionContainer . Exception ;
319328 }
320329 else
@@ -1129,7 +1138,14 @@ object ManageJumpStatementsOrExpressionEval(string expression)
11291138
11301139 if ( expression . StartsWith ( "throw " , StringComparisonForCasing ) )
11311140 {
1132- throw Evaluate ( expression . Remove ( 0 , 6 ) ) as Exception ;
1141+ if ( Evaluate ( expression . Remove ( 0 , 6 ) ) is Exception exception )
1142+ {
1143+ ExceptionDispatchInfo . Capture ( exception ) . Throw ( ) ;
1144+ }
1145+ else
1146+ {
1147+ throw new ExpressionEvaluatorSyntaxErrorException ( "throw keyword must be follow by an Exception instance" ) ;
1148+ }
11331149 }
11341150
11351151 expression = returnKeywordRegex . Replace ( expression , match =>
@@ -1595,7 +1611,8 @@ public object Evaluate(string expression)
15951611
15961612 try
15971613 {
1598- ExpressionEvaluationEventArg expressionEvaluationEventArg = new ExpressionEvaluationEventArg ( expression , this ) ;
1614+ ExpressionEvaluationEventArg expressionEvaluationEventArg =
1615+ new ExpressionEvaluationEventArg ( expression , this ) ;
15991616
16001617 ExpressionEvaluating ? . Invoke ( this , expressionEvaluationEventArg ) ;
16011618
@@ -1618,7 +1635,8 @@ public object Evaluate(string expression)
16181635
16191636 if ( ! s . Trim ( ) . Equals ( string . Empty ) )
16201637 {
1621- throw new ExpressionEvaluatorSyntaxErrorException ( $ "Invalid character [{ ( int ) s [ 0 ] } :{ s } ]") ;
1638+ throw new ExpressionEvaluatorSyntaxErrorException (
1639+ $ "Invalid character [{ ( int ) s [ 0 ] } :{ s } ]") ;
16221640 }
16231641 }
16241642 }
@@ -1637,6 +1655,10 @@ public object Evaluate(string expression)
16371655
16381656 return result ;
16391657 }
1658+ catch ( TargetInvocationException exception ) when ( exception . InnerException != null )
1659+ {
1660+ ExceptionDispatchInfo . Capture ( exception . InnerException ) . Throw ( ) ;
1661+ }
16401662 finally
16411663 {
16421664 evaluationStackCount -- ;
@@ -3088,7 +3110,9 @@ protected virtual bool EvaluateString(string expression, Stack<object> stack, re
30883110 object obj = Evaluate ( innerExp . ToString ( ) ) ;
30893111
30903112 if ( obj is BubbleExceptionContainer bubbleExceptionContainer )
3091- throw bubbleExceptionContainer . Exception ;
3113+ {
3114+ ExceptionDispatchInfo . Capture ( bubbleExceptionContainer . Exception ) . Throw ( ) ;
3115+ }
30923116
30933117 resultString . Append ( obj ) ;
30943118 }
@@ -3283,15 +3307,16 @@ void EvaluateFirstPreviousUnaryOp(int j)
32833307 {
32843308 if ( item is BubbleExceptionContainer bubbleExceptionContainer )
32853309 {
3286- throw bubbleExceptionContainer . Exception ; //Throw the first occuring error
3310+ //Throw the first occuring error
3311+ ExceptionDispatchInfo . Capture ( bubbleExceptionContainer . Exception ) . Throw ( ) ;
32873312 }
32883313 }
32893314 throw new ExpressionEvaluatorSyntaxErrorException ( "Syntax error. Check that no operator is missing" ) ;
32903315 }
32913316 else if ( evaluationStackCount == 1 && stack . Peek ( ) is BubbleExceptionContainer bubbleExceptionContainer )
32923317 {
32933318 //We reached the top level of the evaluation. So we want to throw the resulting exception.
3294- throw bubbleExceptionContainer . Exception ;
3319+ ExceptionDispatchInfo . Capture ( bubbleExceptionContainer . Exception ) . Throw ( ) ;
32953320 }
32963321
32973322 return stack . Pop ( ) ;
@@ -3364,7 +3389,9 @@ protected virtual object ManageKindOfAssignation(string expression, ref int inde
33643389 }
33653390
33663391 if ( result is BubbleExceptionContainer exceptionContainer )
3367- throw exceptionContainer . Exception ;
3392+ {
3393+ ExceptionDispatchInfo . Capture ( exceptionContainer . Exception ) . Throw ( ) ;
3394+ }
33683395
33693396 if ( stack != null )
33703397 {
@@ -3395,9 +3422,9 @@ protected virtual void AssignVariable(string varName, object value)
33953422 {
33963423 Variables [ varName ] = Convert . ChangeType ( value , stronglyTypedVariable . Type ) ;
33973424 }
3398- catch
3425+ catch ( Exception exception )
33993426 {
3400- throw new InvalidCastException ( $ "A object of type { typeToAssign } can not be cast implicitely in { stronglyTypedVariable . Type } ") ;
3427+ throw new InvalidCastException ( $ "A object of type { typeToAssign } can not be cast implicitely in { stronglyTypedVariable . Type } ", exception ) ;
34013428 }
34023429 }
34033430 }
@@ -4117,8 +4144,10 @@ protected virtual Type GetTypeByFriendlyName(string typeName, string genericType
41174144 }
41184145 }
41194146 }
4120- catch ( ExpressionEvaluatorSyntaxErrorException )
4147+ catch ( ExpressionEvaluatorSyntaxErrorException exception )
41214148 {
4149+ ExceptionDispatchInfo . Capture ( exception ) . Throw ( ) ;
4150+ // Will not go here but Resharper detect some errors because he does not know ExceptionDispatchInfo stuff.
41224151 throw ;
41234152 }
41244153 catch { }
0 commit comments