473,405 Members | 2,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

How to find which object cause an exception?

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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #1
5 2102
Put a constant at the class level, concatenate it to the error description
as you're throwing it in your catch.

--
--itai
"Benny" <an*******@devdex.com> wrote in message
news:uf*************@tk2msftngp13.phx.gbl...
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #2
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();
}

"Benny" <an*******@devdex.com> wrote in message
news:uf*************@tk2msftngp13.phx.gbl...
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #3
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.

--
--itai
"Benny" <an*******@devdex.com> wrote in message
news:uf*************@tk2msftngp13.phx.gbl...
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #4
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
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #5
AFAIK only with the help of call stack...

"Benny" <an*******@devdex.com> wrote in message
news:uA**************@TK2MSFTNGP10.phx.gbl...
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
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: mike p. | last post by:
I have a docbook xml file, and am using standard docbook 1.61.3 xsl stylesheets to do xhtml transform. Transform works fine when using MSXML. When I try to do the following using asp.net 1.1: ...
1
by: Robert A Riedel | last post by:
I am completely baffled when the following managed exception is thrown: "Object reference not set to an instance of an object" from a nested subroutine when referencing a variable allocated on the...
0
by: Sam Fields | last post by:
I have found very little regarding the error "Unable to find an entry point named EnumerateSecurityPackagesW in DLL security.dll. ". I have an ASP.NET Web Service being accessed via SSL. I found...
4
by: Arapi | last post by:
I am having a very strange problem with my asp.net application. I am getting error: 'Object reference not set to an instance of an object' when I run my app from my hosting company. I have...
2
by: Hennie | last post by:
I Get the following error all of a sudden: I do not know what is wrong or where to start looking. I added the Debug="true" directive at the top of the page, but it does not help at all. What...
3
by: red_ghost | last post by:
in my home.aspx page I have a simple login set. when you hit the button it redirects. Here is the code: Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs)...
8
by: fniles | last post by:
I have a collection inside a class, sometimes when I add to the collection, I get the error "At least one object must implement IComparable". What does the error mean ? Thanks. Public Class...
2
by: karinmorena | last post by:
I'm having 4 errors, I'm very new at this and I would appreciate your input. The error I get is: Week5MortgageGUI.java:151:cannot find symbol symbol: method allInterest(double,double,double)...
4
by: shall | last post by:
I have several pages ( ASP) that were working OK on NT4. We moved it to Server 2003. At random , it appears that the Server.CreateObject fails. I've looked through the code and it looks OK. ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.