How to determine if an exception is of a particular type

前端 未结 5 1102
Happy的楠姐
Happy的楠姐 2021-02-01 00:21

I have a piece of try catch code:

try 
{
    ...
}
catch(Exception ex) 
{
    ModelState.AddModelError(
        \"duplicateInvoiceNumberOrganisation\", \"The com         


        
5条回答
  •  长发绾君心
    2021-02-01 00:54

    You can take a look at the SQLException class -- and check for the contents of the exception's message if it contains what you now see in your inner exception..Something like this:

    try
    {
        //your code here
    }
    catch (SQLException ex)
    {
        if (ex.Message.Contains("Cannot insert duplicate key in obj...."))
        {
            //your code here
        }
    }
    

提交回复
热议问题