How to tell which kind of exeption it is?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi, friends,

We have a common exception log function in C#:

public void LogException(System.Exception e)
{
.......//write error info to a file
}

which will log error message into a file. Sometiems, SqlException thrown
when using ADO.net will also be passed into this function.

I really want to log more info for those DB errors. However, how do I tell a
passed in exeption is a SqlExeption or other kind of exeptions?

Thanks a lot
 
Andrew said:
Hi, friends,

We have a common exception log function in C#:

public void LogException(System.Exception e)
{
.......//write error info to a file
}

which will log error message into a file. Sometiems, SqlException thrown
when using ADO.net will also be passed into this function.

I really want to log more info for those DB errors. However, how do I tell a
passed in exeption is a SqlExeption or other kind of exeptions?

Same way as you check for any other object's type - using either the
"as" operator or the "is" operator. (You could call GetType(), but
usually using "as" or "is" is the right way to go.)
 
You can have multiple catch blocks, one for each exception type

Jeff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top