How to determine if an exception is of a particular type

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

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

提交回复
热议问题