简体   繁体   English

在ASP.NET MVC和C#中使用自定义异常类时未捕获异常

[英]Exception not catching when using Custom Exception classes in ASP.NET MVC and C#

I have created few custom exception class 我创建了一些自定义异常类

public class CreateNewUserWebException : Exception
{
    public CreateNewUserWebException(string email): base(
        string.Format("[{0}] - User could not be added.", email))
    {
    }
}
public class CreateNewUserEntityFrameworkException : System.Data.DataException
{
    public CreateNewUserEntityFrameworkException(string email)
        : base(
            string.Format("[{0}] - User could not be added.", email))
    {
    }
}

and here is my controller code 这是我的控制器代码

try
{
    var user = _createUserModule.CreateUser(model);
    CookieManager.SetAuthenticationCookie(user, model.Email, rememberMe: false);
    return RedirectToAction("Index", "Bugs");
}
catch (CreateNewUserEntityFrameworkException exception)
{
    this.ModelState.AddModelError("", "Some error occured while registering you on our sytem. Please try again later.");
    Elmah.ErrorSignal.FromCurrentContext().Raise(exception);
}
catch (CreateNewUserWebException exception)
{
    this.ModelState.AddModelError("", "Some error occured while registering you on our sytem. Please try again later.");
    Elmah.ErrorSignal.FromCurrentContext().Raise(exception);
}
catch(Exception exception)
{
    this.ModelState.AddModelError("", "Some error occured while registering you on our sytem. Please try again later.");
    Elmah.ErrorSignal.FromCurrentContext().Raise(exception);
}

I have purposely fully induced an primary key violation exception which is 我故意完全诱发了一个主键冲突异常,它是

在此处输入图片说明

but exception is not catched by my custom exception class. 但是我的自定义异常类未捕获异常。 It is not caught by the last exception catch block. 它没有被最后一个异常捕获块捕获。

I cannot understand why so. 我不明白为什么会这样。 Can some one help me out on this please. 有人可以帮我这个忙吗?

The part you've highlighted in the debugger is the inner exception. 您在调试器中突出显示的部分是内部异常。 That isn't used by the CLR to determine which catch block to enter. CLR不会使用它来确定要输入哪个catch块。 The outer exception is just a DbUpdateException - which you haven't specified a particular catch block for. 外部异常只是DbUpdateException您尚未DbUpdateException指定特定的catch块。

Even the inner exception is just a DataException - it's not an instance of your custom exception. 甚至内部异常也只是DataException它不是您的自定义异常的实例。

You haven't shown any code which actually throws your exception - are you sure it's being used at all? 您没有显示任何实际引发异常的代码-您确定它已被使用吗? What code have you written to tell EF to throw your exception rather than the exception it would otherwise throw? 您编写了什么代码来告诉EF引发您的异常,而不是本应引发的异常?

(Given your comments, I'm not sure you quite understand exception handling. Creating a custom exception doesn't somehow let you catch an instance of that without it being thrown - something still has to throw an instance of that exception before it's any use.) (鉴于您的评论,我不确定您是否完全理解异常处理。创建自定义异常不会以某种方式让您在不引发该异常的情况下捕获该异常的实例-在使用该异常之前,仍然需要抛出该异常的实例)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM