Share via


how to compare exceptions? [exceptions/types]

Question

Wednesday, October 1, 2008 8:11 AM

Hi, I need to compare exceptions
How do you do that?

This code doesnt work:

catch (SoapException e) 
            { 
 
                      SqlException e2 = new SqlException(); 
                    if (e.InnerException == e2 ) 
                       Console.Write("This was Sql Exception"); 
                    // else if ... 
            } 

If I do like this:

 if (e.InnerException == SqlException) ; 

I got Error:        'System.Data.SqlClient.SqlException' is a 'type' but is used like a 'variable'   

Thanks in advance

All replies (3)

Wednesday, October 1, 2008 8:24 AM âś…Answered | 3 votes

Hi

try:

if(e.InnerException.GetType() == typeof(SqlException))  
.... 

Regards

Alex


Wednesday, October 1, 2008 9:10 AM

yep,

Thanks a lot!


Tuesday, August 27, 2013 12:05 AM | 1 vote

That'll work, however I think this is slightly more succinct...

if (e.InnerException is SqlException)

Mick Lang