How to find which object cause an exception?

  • Thread starter Thread starter Benny
  • Start date Start date
B

Benny

Hello Experts,

In C#, how can I find out which object (i.e. return me the object name)
causes the exception error in the try..catch... statement? I want to
display the object that caused the exception in the error message.

Thanks in advanced,

Benny
 
Put a constant at the class level, concatenate it to the error description
as you're throwing it in your catch.
 
Oh, you can also use get the name of the calling method from the exception
object, but I don't see why you'd want to bother with that. A simple
constant will do the trick with less complication.
 
if you can identify your object than just put this information in the
Exception object

and if you cannot identify it, for example because of there a lot code in
the try statement, than you can show the part of the call stack, but AFAIK
you will not be able to obtaint the particular object
For example

public class MyObj
{
public long m_id;

public MyObj(long id)
{
m_id = id;
}

public void DoSomething()
{

}
}

MyObj obj = new MyObj(5)
try
{
obj.DoSomething();
}
catch
{
if (obj != null)
Log("Error with " + obj.m_id.ToString();
}
 
For example:

...
try
{
string x = dropdownlistA.selecteditem.value;
string y = dropdownlistB.selecteditem.value;
string z = dropdownlistC.selecteditem.value;
}
catch( exception ex )
{
...
}
...

A null reference exception error may causes by any of these 3 lins of
code (x, y, or z). How can I find out which object (dropdownlistA,
dropdownlistB, or dropdownlistC) caused the error?


Thanks,

Benny
 

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