How to determine if an exception is of a particular type

前端 未结 5 485
误落风尘
误落风尘 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:49

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

提交回复
热议问题