How to determine if an exception is of a particular type

C#asp.net MvcException Handling

C# Problem Overview


I have a piece of try catch code:

try 
{
    ...
}
catch(Exception ex) 
{
    ModelState.AddModelError(
        "duplicateInvoiceNumberOrganisation", "The combination of organisation and invoice number must be unique");
}

For this piece of code I'm trying to insert a record into a database: The dba has set it up so that the database checks for duplicates and returns an error if there are duplicates. Currently, as you can see, I'm adding the same error to the model no matter what error occurred. I want it changed so this error is only added to the model if it was caused by the duplicate error set up by the dba.

Below is the error I want to catch. Note it's in the inner exception. Can anyone tell me how to specifically catch this one?

enter image description here

C# Solutions


Solution 1 - C#

before your current catch add the following:

catch(DbUpdateException ex)
{
  if(ex.InnerException is UpdateException)
  {
    // do what you want with ex.InnerException...
  }
}

From C# 6, you can do the following:

catch(DbUpdateException ex) when (ex.InnerException is UpdateException)
{
    // do what you want with ex.InnerException...
}

Solution 2 - C#

Replace System.Threading.ThreadAbortException with your exception.

try
{
    //assume ThreadAbortException occurs here
}
catch (Exception ex)
{
    if (ex.GetType().IsAssignableFrom(typeof(System.Threading.ThreadAbortException)))
    {
         //what you want to do when ThreadAbortException occurs         
    }
    else
    {
         //do when other exceptions occur
    }
}

Solution 3 - C#

Not enough rep to comment. In response to @conterio question (in @Davide Piras answer):

> is there a catch "when not" syntax?

There is.

catch (Exception e) when (!(e is ArgumentException)) { }

Solution 4 - C#

To get name of the exception you can use

    catch (Exception exc){
       if (exc.GetType().FullName == "Your_Exception") 
       {
          // The same can be user for InnerExceptions
          // exc.InnerException.GetType().FullName
       }
   }

Solution 5 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnonyMouseView Question on Stackoverflow
Solution 1 - C#Davide PirasView Answer on Stackoverflow
Solution 2 - C#NishanthaView Answer on Stackoverflow
Solution 3 - C#JasonView Answer on Stackoverflow
Solution 4 - C#Uday DesirajuView Answer on Stackoverflow
Solution 5 - C#Ann B. G.View Answer on Stackoverflow