首页 > 解决方案 > 在 Invoked 方法中抛出异常时,特定异常捕获块不起作用

问题描述

在下面的代码中,我有一个 System.UnauthorizedAccessException 类异常的 catch 块。

           try
            {

                var type = message.Form.GetObjTypeOf(typeof(BaseEntity));  

                MethodInfo updateFormStatusMethod = typeof(IFormService).GetMethod("UpdateFormStatus");
                MethodInfo generic = updateFormStatusMethod.MakeGenericMethod(type);
                var result = (bool)generic.Invoke(performanceformService, new object[] { message });
             // I throw new System.UnauthorizedAccessException() inside 'UpdateFormStatus' method

            }
            catch (System.UnauthorizedAccessException)
            {
               // I'm waiting to get 'UnauthorizedAccessException' here
            }
            catch (System.Exception ex)
            {
               //But I get it here
            }

我正在等待在特定的 catch 块中获得“UnauthorizedAccessException”,但我在一般的 catch 块中得到它。

标签: c#

解决方案


推荐阅读