How to determine if an exception is of a particular type

前端 未结 5 486
误落风尘
误落风尘 2021-02-01 00:32

I have a piece of try catch code:

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


        
5条回答
  •  梦如初夏
    2021-02-01 00:55

    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
        }
    }
    

提交回复
热议问题