Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
I want to determine the Exception type inside my catch block:
try 
{
    // ...
}
catch (Exception ex)
{
    if (ex == UnauthorizedAccessException)  // ??
    {
        // ...
    }
    else
    {
        // ...
    }
}
Posted
Updated 11-Dec-10 1:33am
v2
Comments
Abdul Quader Mamun 11-Dec-10 6:36am    
Use pre tag

You can try like bellow code.

C#
try
{
    // do something that could throw an exception
}
catch(Exception e)
{
    if (e is FileNotFoundException)
    {
        // •••
    }
    else if (e is DivideByZeroException)
    {
        // •••
    }
    else throw;
}



Thanks
 
Share this answer
 
v2
You can use GetType() function to determine which exception is generate in Catch block.
try
         {

         }
         catch (Exception ex)
         {
             MessageBox.Show(ex.GetType().ToString()); // It will Display Message type
         }


:)
 
Share this answer
 
v2
C#
try
{
    // ...
}
catch (UnauthorizedAccessException)
{
    // ...
}
catch (Exception)
{
    // ...
}

Something like this.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900