小编BIL*_*MAD的帖子

为什么使用特定的异常catch块

在下面的代码中,我有一个System.Data.Entity.Infrastructure.DbUpdateException类异常的catch块.

我的问题是为什么我不能使用Exception类来捕获我的代码中的每个可能的异常并获得stacktrace?

特定异常类型的优点是什么,以及它们在多个catch块中的使用?

try
{
    AddAdminUserInput input1 = JsonConvert.DeserializeObject<AddAdminUserInput>(input);
    Foundation_Services_DL_DataEntities Db = DLMetadataContext.GetContext();
    UserAccount account = new UserAccount
    {
        emplid = input1.emplid,
        sso = input1.sso,
        deptid = input1.deptid,
        usertype = input1.usertype,
        status = input1.status,
        username = input1.username
    };

    Db.UserAccounts.Add(account);
    Db.SaveChanges();

    Dictionary<string, string> dict = new Dictionary<string, string>();
    dict.Add("status", "0");
    dict.Add("message", "User Addition Successful");

    Context.Response.Write(JsonConvert.SerializeObject(dict));
}
catch (System.Data.Entity.Infrastructure.DbUpdateException dbev)
{
    Dictionary<string, string> dict = new Dictionary<string, string>();
    dict.Add("status", "1");
    dict.Add("message", "User Addition Failed - User Already Exists"); …
Run Code Online (Sandbox Code Playgroud)

c# asp.net exception-handling

6
推荐指数
1
解决办法
449
查看次数

标签 统计

asp.net ×1

c# ×1

exception-handling ×1