473,387 Members | 1,530 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,387 software developers and data experts.

Question on usage guidelines for Exception.TargetSite

In designing a new control for general use, I have come across a design
decision involving exceptions...

I know that TargetSite returns the name of the method that threw the
exception; however, will users of the control expect that TargetSite always
reflects the method of the control that they actually called? For example,
consider a method on my class named "publicfoo" that calls some internal
method name "internalbar" (which includes some generalized processing used
by other methods in addition to "publicfoo"). "internalbar" raises an
exception which needs to propagate back to the caller of "publicfoo".

If the user of my control catches the exception, the TargetSite will contain
"internalbar", a method that the end-user knows nothing about. I realize
"publicfoo" will be in the stack trace, but that requires parsing through it
to find the actual method that the control user invoked.

Am I trying to use TargetSite inappropriately here? It seems 'redundant' to
add a MethodName argument to all of my own exceptions and would require
subclassing System exceptions (like InvalidArgumentException) if I want to
provide the MethodName consistently.

I also thought about having "internalbar" return the exception to
"publicfoo" and then "publicfoo" can throw it; however, that seems contrary
to good programming encapsulation and would necessitate logic in all methods
like "publicfoo" to not only throw the exception, but first detect whether
an exception occurred and then throw it. It seems like a potential area for
bugs to creep in, since I (or maintenance programmers) would need to
remember to do this check in any new public method as well.

There seems to be some precedent in ADO.NET for which TargetSite does indeed
report an internal method: when calling OleDbCommand.ExecuteReader, without
an associated connection/transaction, an exception will be thrown in an
internal method called ValidateConnectionAndTransaction and that will be
reported back to the invoking code within the TargetSite property. The
programmer in this case though called ExecuteReader.

In looking at some of the Microsoft Documentation for Class Developers, I
haven't happened across anything that specifies what the 'best-practice' is
in this case. What are your experiences when using other .Net classes?

Jul 21 '05 #1
1 3987
The issue is that, there can be any number of calls on the stack until the
method that actually threw the error. The method that threw the error is
the one in TargetSite, regardless of whether or not it is a framework
method.

What you can do, is create an instance of the StackTrace class from the
exception object you have. Then, you can examine every frame of the stack
trace, look at the function name, assembly name, etc, and figure out which
one is the one you want to display to the user.

"Jim O'Neil [Sybase]" <jo****@sybase.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
In designing a new control for general use, I have come across a design
decision involving exceptions...

I know that TargetSite returns the name of the method that threw the
exception; however, will users of the control expect that TargetSite always reflects the method of the control that they actually called? For example, consider a method on my class named "publicfoo" that calls some internal
method name "internalbar" (which includes some generalized processing used
by other methods in addition to "publicfoo"). "internalbar" raises an
exception which needs to propagate back to the caller of "publicfoo".

If the user of my control catches the exception, the TargetSite will contain "internalbar", a method that the end-user knows nothing about. I realize
"publicfoo" will be in the stack trace, but that requires parsing through it to find the actual method that the control user invoked.

Am I trying to use TargetSite inappropriately here? It seems 'redundant' to add a MethodName argument to all of my own exceptions and would require
subclassing System exceptions (like InvalidArgumentException) if I want to
provide the MethodName consistently.

I also thought about having "internalbar" return the exception to
"publicfoo" and then "publicfoo" can throw it; however, that seems contrary to good programming encapsulation and would necessitate logic in all methods like "publicfoo" to not only throw the exception, but first detect whether
an exception occurred and then throw it. It seems like a potential area for bugs to creep in, since I (or maintenance programmers) would need to
remember to do this check in any new public method as well.

There seems to be some precedent in ADO.NET for which TargetSite does indeed report an internal method: when calling OleDbCommand.ExecuteReader, without an associated connection/transaction, an exception will be thrown in an
internal method called ValidateConnectionAndTransaction and that will be
reported back to the invoking code within the TargetSite property. The
programmer in this case though called ExecuteReader.

In looking at some of the Microsoft Documentation for Class Developers, I
haven't happened across anything that specifies what the 'best-practice' is in this case. What are your experiences when using other .Net classes?

Jul 21 '05 #2

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

Similar topics

4
by: Steven | last post by:
I just downloaded the June 2005 release and I want to set up basic logging to log problems such as exceptions. I have created a category named Exceptions with the following: <category...
5
by: Vitling | last post by:
For no apparent reason, a NullReference exception is thrown in system.dll (System.Net.Sockets.OverlappedAsyncResult.CompletionPortCallback). Since I only get a disassembly from Visual Studio, it...
3
by: Lekyan | last post by:
I have problem setting the password for an ADAM user using C#. I used the SetPassword code given in the Microsoft page, changed several parameters, but ran into an exception. I wonder if other...
39
by: Patrick | last post by:
The c# code style guide that I follow suggests that class variables (fields) be coded with camel casing, like this: int recordId; string name; It also suggests that variables within methods...
11
by: Matt | last post by:
I have written a console application that runs as a batch job on Windows 2000 Server. Every once in a while I am getting an exception thrown to the console. Is there a way I can create a global...
1
by: Jim O'Neil [Sybase] | last post by:
In designing a new control for general use, I have come across a design decision involving exceptions... I know that TargetSite returns the name of the method that threw the exception; however,...
3
by: Corey B | last post by:
I have added code to my Application_Error sub in Global.ASAX so that whenever an error occurs in my ASP.NET application - it sends me an email with the error information. However the information...
0
by: =?Utf-8?B?YmlqYXk=?= | last post by:
The type initializer for 'Microsoft.ApplicationBlocks.Cache.CacheService' threw an exception. We migrated our windows application from 1.1 to 2.0. The debug and Release mode of the application...
4
by: =?Utf-8?B?YmlqYXk=?= | last post by:
We migrated our windows application from 1.1 to 2.0. The debug and Release mode of the application work fine with some tweaking. But when the setup project is migrated to 2.0 the installation gives...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.