473,805 Members | 2,007 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question on usage guidelines for Exception.Targe tSite

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 "internalba r" (which includes some generalized processing used
by other methods in addition to "publicfoo" ). "internalba r" 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
"internalba r", 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 InvalidArgument Exception) if I want to
provide the MethodName consistently.

I also thought about having "internalba r" 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.Ex ecuteReader, without
an associated connection/transaction, an exception will be thrown in an
internal method called ValidateConnect ionAndTransacti on 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 4023
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******** ********@TK2MSF TNGP11.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 "internalba r" (which includes some generalized processing used
by other methods in addition to "publicfoo" ). "internalba r" 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 "internalba r", 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 InvalidArgument Exception) if I want to
provide the MethodName consistently.

I also thought about having "internalba r" 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.Ex ecuteReader, without an associated connection/transaction, an exception will be thrown in an
internal method called ValidateConnect ionAndTransacti on 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
4629
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 name="Exceptions"> <destinations> <destination name="Event Log Destination" sink="Event Log Sink" format="Text Formatter" /> </destinations> </category>
5
4032
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 is almost impossible to figure out what causes this. I've tried adding: AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler (SystemErrorHandler); to my main method, and a handler function:
3
7852
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 people have solved this problem. But I have searched Google groups for a couple days but still couldn't get a solution. I copied the code from: ...
39
3514
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 and method parameters use camel casing, like this: void SetName(int id, string newName)
11
19180
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 handler that can catch this exception and prevent the application from locking up? I am performing try catch statements at all database and file write calls so I am unsure where it is coming from. Any suggestions or recommendations are greatly...
1
236
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, 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...
3
1545
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 that I am receiving doesn't have everything I would like. It doesn't give the exact line of code and the exact place where the error occurred. Can anyone help me with the code? I would like it to send me the same type of information that you...
0
1982
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 work fine with some tweaking. But when the setup project is migrated to 2.0 the installation gives the follwing error: - <ExceptionInformation> <AdditionalInformationProperty ExceptionManager.MachineName="TestDev"...
4
39707
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 the follwing error: Message:The type initializer for 'Microsoft.ApplicationBlocks.Cache.CacheService' threw an exception. - <ExceptionInformation>
0
9716
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10604
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10356
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10361
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10103
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7644
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6874
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4316
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3839
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.