17
17
using System . Linq . Expressions ;
18
18
using System . Reflection ;
19
19
using System . Runtime . CompilerServices ;
20
+ using System . Runtime . ExceptionServices ;
20
21
using System . Runtime . InteropServices ;
21
22
using System . Text ;
22
23
using System . Text . RegularExpressions ;
@@ -287,15 +288,17 @@ protected enum TryBlockEvaluatedState
287
288
{ ExpressionOperator . ConditionalAnd , ( dynamic left , dynamic right ) => {
288
289
if ( left is BubbleExceptionContainer leftExceptionContainer )
289
290
{
290
- throw leftExceptionContainer . Exception ;
291
+ leftExceptionContainer . Throw ( ) ;
292
+ return null ; // this line is never reached
291
293
}
292
294
else if ( ! left )
293
295
{
294
296
return false ;
295
297
}
296
298
else if ( right is BubbleExceptionContainer rightExceptionContainer )
297
299
{
298
- throw rightExceptionContainer . Exception ;
300
+ rightExceptionContainer . Throw ( ) ;
301
+ return null ; // this line is never reached
299
302
}
300
303
else
301
304
{
@@ -308,15 +311,17 @@ protected enum TryBlockEvaluatedState
308
311
{ ExpressionOperator . ConditionalOr , ( dynamic left , dynamic right ) => {
309
312
if ( left is BubbleExceptionContainer leftExceptionContainer )
310
313
{
311
- throw leftExceptionContainer . Exception ;
314
+ leftExceptionContainer . Throw ( ) ;
315
+ return null ; // this line is never reached
312
316
}
313
317
else if ( left )
314
318
{
315
319
return true ;
316
320
}
317
321
else if ( right is BubbleExceptionContainer rightExceptionContainer )
318
322
{
319
- throw rightExceptionContainer . Exception ;
323
+ rightExceptionContainer . Throw ( ) ;
324
+ return null ; // this line is never reached
320
325
}
321
326
else
322
327
{
@@ -2147,20 +2152,15 @@ where method.GetParameters()[0].ParameterType == objType // static extMethod(thi
2147
2152
}
2148
2153
catch ( NullReferenceException nullException )
2149
2154
{
2150
- stack . Push ( new BubbleExceptionContainer ( )
2151
- {
2152
- Exception = nullException
2153
- } ) ;
2155
+ stack . Push ( new BubbleExceptionContainer ( nullException ) ) ;
2154
2156
2155
2157
return true ;
2156
2158
}
2157
2159
catch ( Exception ex )
2158
2160
{
2159
2161
//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 ) ) ;
2164
2164
return true ; //Signals an error to the parsing method array call
2165
2165
}
2166
2166
}
@@ -2410,10 +2410,8 @@ where method.GetParameters()[0].ParameterType == objType // static extMethod(thi
2410
2410
catch ( Exception ex )
2411
2411
{
2412
2412
//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 ) ) ;
2417
2415
i -- ;
2418
2416
return true ; //Signals an error to the parsing method array call
2419
2417
}
@@ -3082,7 +3080,7 @@ protected virtual bool EvaluateString(string expression, Stack<object> stack, re
3082
3080
object obj = Evaluate ( innerExp . ToString ( ) ) ;
3083
3081
3084
3082
if ( obj is BubbleExceptionContainer bubbleExceptionContainer )
3085
- throw bubbleExceptionContainer . Exception ;
3083
+ bubbleExceptionContainer . Throw ( ) ;
3086
3084
3087
3085
resultString . Append ( obj ) ;
3088
3086
}
@@ -3181,7 +3179,7 @@ void EvaluateFirstNextUnaryOp(int j, ref int parentIndex)
3181
3179
}
3182
3180
else
3183
3181
{
3184
- list [ i ] = new BubbleExceptionContainer ( ) { Exception = ex } ; //Transport the processing error
3182
+ list [ i ] = new BubbleExceptionContainer ( ex ) ; //Transport the processing error
3185
3183
}
3186
3184
}
3187
3185
list . RemoveAt ( i - 1 ) ;
@@ -3216,7 +3214,7 @@ void EvaluateFirstPreviousUnaryOp(int j)
3216
3214
}
3217
3215
else
3218
3216
{
3219
- list [ i ] = new BubbleExceptionContainer ( ) { Exception = ex } ; //Transport the processing error
3217
+ list [ i ] = new BubbleExceptionContainer ( ex ) ; //Transport the processing error
3220
3218
}
3221
3219
}
3222
3220
list . RemoveAt ( i + 1 ) ;
@@ -3252,7 +3250,7 @@ void EvaluateFirstPreviousUnaryOp(int j)
3252
3250
}
3253
3251
else
3254
3252
{
3255
- list [ i ] = new BubbleExceptionContainer ( ) { Exception = ex } ; //Transport the processing error
3253
+ list [ i ] = new BubbleExceptionContainer ( ex ) ; //Transport the processing error
3256
3254
}
3257
3255
}
3258
3256
list . RemoveAt ( i + 1 ) ;
@@ -3277,15 +3275,15 @@ void EvaluateFirstPreviousUnaryOp(int j)
3277
3275
{
3278
3276
if ( item is BubbleExceptionContainer bubbleExceptionContainer )
3279
3277
{
3280
- throw bubbleExceptionContainer . Exception ; //Throw the first occuring error
3278
+ bubbleExceptionContainer . Throw ( ) ; //Throw the first occuring error
3281
3279
}
3282
3280
}
3283
3281
throw new ExpressionEvaluatorSyntaxErrorException ( "Syntax error. Check that no operator is missing" ) ;
3284
3282
}
3285
3283
else if ( evaluationStackCount == 1 && stack . Peek ( ) is BubbleExceptionContainer bubbleExceptionContainer )
3286
3284
{
3287
3285
//We reached the top level of the evaluation. So we want to throw the resulting exception.
3288
- throw bubbleExceptionContainer . Exception ;
3286
+ bubbleExceptionContainer . Throw ( ) ;
3289
3287
}
3290
3288
3291
3289
return stack . Pop ( ) ;
@@ -3358,7 +3356,7 @@ protected virtual object ManageKindOfAssignation(string expression, ref int inde
3358
3356
}
3359
3357
3360
3358
if ( result is BubbleExceptionContainer exceptionContainer )
3361
- throw exceptionContainer . Exception ;
3359
+ exceptionContainer . Throw ( ) ;
3362
3360
3363
3361
if ( stack != null )
3364
3362
{
@@ -4692,7 +4690,14 @@ public partial class MethodsGroupEncaps
4692
4690
4693
4691
public partial class BubbleExceptionContainer
4694
4692
{
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 ( ) ;
4696
4701
}
4697
4702
4698
4703
public partial class ExpressionEvaluatorSyntaxErrorException : Exception
0 commit comments