473,583 Members | 3,571 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why Does Asp.Net Use The Innermost Exception?

First let me say that I understand that Asp.Net wraps my exception in an
HttpUnhandledEx ception. I have found a lot of discussion about that on the
web, which was informative, but not helpful. Let me explain how my question
is different.

Second, let me say that I have two questions. One is an ASP.Net related
question. The other is a general System.Exceptio n question. They are related
but also individual.

Okay here is my problem...

I have an ADO wrapper library. I use it to wrap provider-specific exceptions
with my own. For example when a opening a connection fails, my library has
its own exception that it throws. Consumers of my library don't know if it
was an Oracle connection that failed, an SqlClient connection that failed,
etc. However just in case, I do put the original ADO provider's exception in
the *innerException * property of my library's exception.

When an exception is thrown by my library, and not handled in the aspx page,
IIS displays the default error screen, which is fine. But it displays the
*inner-most* exception. So in this case, instead of displaying my library's
exception, it displays the provider's exception, which I had put into my
library's exception's innerException property. This is not what I want. I
don't want consumers of my library to see the provider exception unless they
really want to.

Like I said at the top of my post, I understand that it would not display
the outer-most exception. That would be the HttpUnhandledEx ception. But I
don't understand why it drills down to the inner-most exception.

So I started poking around...

I stopped populating my library's exception's innerException. Now IIS
displayed my exception just like I wanted. When there is no innerException,
IIS displays my exception.

I wondered if there was something special about the provider exception that
made IIS want to display it, instead of displaying mine. So I populated my
library's exception's innerException with a new dummy exception. IIS still
displayed the inner-most exception.

Feeling frustrated I wondered if GetBaseExceptio n was being called. So I
overrode it. *Success!* If I return a new dummy exception from my library's
overridden GetBaseExceptio n method, that is what shows up in IIS. So clearly,
yes, IIS is showing the inner-most exception.

Why does IIS take it upon itself to decide that the inner-most exception
should be displayed? This makes me mad. Why would I wrap one exception inside
another if I don't want the outer exception to be exposed to the world? I
fully understand that IIS does not want to display the outer-most exception
(the httpUnhandledEx ception).

Thinking, I override GetBaseExceptio n to return "me" (c# syntax "this"). It
doesn't work. I'm not sure what is going on. When I am not sure what is going
on I always turn to my trust friend the .Net Reflector!
(http://www.aisto.com/roeder/dotnet/). Sure enough take a look at this little
method known as System.Exceptio n.GetBaseExcept ion()...

Public Overridable Function GetBaseExceptio n() As Exception
Dim exception1 As Exception = Me.InnerExcepti on
Dim exception2 As Exception = Me
Do While (Not exception1 Is Nothing)
exception2 = exception1
exception1 = exception1.Inne rException
Loop
Return exception2
End Function

Notice anything funny? It does *not* call the innerExceptions 's
GetBaseExceptio n. It just starts drilling down on *it's own* through the
InnerExceptions directly. Why doesn't it call the InnerException' s
GetBaseExceptio n? That wouldn't be recursion exactly, but it would make more
sense to me. As it stands, overriding GetBaseExceptio n only works when you
are the outer-most exception. If the overriding exception gets wrapped up as
an inner exception (say for example in an HttpUnhandledEx ception, as in this
case) all your precious overridden GetBaseExceptio n logic is *out the window*.

So I have two questions...

1) Why does IIS display the GetBaseExceptio n of the HttpUnhandledEx ception
instead of the InnerException?

2) Why does System.Exceptio n.GetBaseExcept ion drill down on its own through
the InnerExceptions , instead of honoring the possibly overridden
GetBaseExceptio ns?

Any information, education, guidance, or I-feel-your-pains would be greatly
appreciated.

John DeHope
Jul 14 '06 #1
3 2501
I'll answer #1...

ASP.NET displays exceptions to help you debug. You are supposed to set
customError="On " or "RemoteOnly " meaning they should only ever been
"exposed" to developers. As you know, when it comes to troubleshooting an
error, the inner exception always contains the most meaningful information.
In other words, I think the reason ASP.NET displays the inner-most exception
is because it's not only the most useful, but also because it should only
been seen by someone actually trying to solve the error.

A lot more people would complain if it _didn't_ display the inner exception.

I know very little about what exactly you're doing, so I won't pass
judgement too quickly, but do consider this .NET design guideline:
"Do not create and throw new exceptions just to have 'your team's'
exception."
from http://blogs.msdn.com/kcwalina/archi...16/396787.aspx
As for GetBaseExceptio n questions, personally, I'd think this is by-design
(ie, they always want the root exception to be returned). But why make it
overridable?!

Well, looking at the docs sheds some light on the issue:

First, it's clear that the design of this is very strict:
"The first exception thrown in a chain of exceptions", and
"For all exceptions in a chain of exceptions, the GetBaseExceptio n method
must return the same object (the base exception)."

So it looks like a pretty well-defined design..but why the overridable,
well:
"Notes to Inheritors The GetBaseExceptio n method is overridden in classes
that require control over the exception content or format."

in other words, it doesn't look like GetBaseExceptio n is there to change the
core behaviour (returning the root exception) but rather the
content/format....
Karl
--
http://www.openmymind.net/
http://www.fuelindustries.com/
"JohnDeHope 3" <Jo*********@di scussions.micro soft.comwrote in message
news:26******** *************** ***********@mic rosoft.com...
First let me say that I understand that Asp.Net wraps my exception in an
HttpUnhandledEx ception. I have found a lot of discussion about that on the
web, which was informative, but not helpful. Let me explain how my
question
is different.

Second, let me say that I have two questions. One is an ASP.Net related
question. The other is a general System.Exceptio n question. They are
related
but also individual.

Okay here is my problem...

I have an ADO wrapper library. I use it to wrap provider-specific
exceptions
with my own. For example when a opening a connection fails, my library has
its own exception that it throws. Consumers of my library don't know if it
was an Oracle connection that failed, an SqlClient connection that failed,
etc. However just in case, I do put the original ADO provider's exception
in
the *innerException * property of my library's exception.

When an exception is thrown by my library, and not handled in the aspx
page,
IIS displays the default error screen, which is fine. But it displays the
*inner-most* exception. So in this case, instead of displaying my
library's
exception, it displays the provider's exception, which I had put into my
library's exception's innerException property. This is not what I want. I
don't want consumers of my library to see the provider exception unless
they
really want to.

Like I said at the top of my post, I understand that it would not display
the outer-most exception. That would be the HttpUnhandledEx ception. But I
don't understand why it drills down to the inner-most exception.

So I started poking around...

I stopped populating my library's exception's innerException. Now IIS
displayed my exception just like I wanted. When there is no
innerException,
IIS displays my exception.

I wondered if there was something special about the provider exception
that
made IIS want to display it, instead of displaying mine. So I populated my
library's exception's innerException with a new dummy exception. IIS still
displayed the inner-most exception.

Feeling frustrated I wondered if GetBaseExceptio n was being called. So I
overrode it. *Success!* If I return a new dummy exception from my
library's
overridden GetBaseExceptio n method, that is what shows up in IIS. So
clearly,
yes, IIS is showing the inner-most exception.

Why does IIS take it upon itself to decide that the inner-most exception
should be displayed? This makes me mad. Why would I wrap one exception
inside
another if I don't want the outer exception to be exposed to the world? I
fully understand that IIS does not want to display the outer-most
exception
(the httpUnhandledEx ception).

Thinking, I override GetBaseExceptio n to return "me" (c# syntax "this").
It
doesn't work. I'm not sure what is going on. When I am not sure what is
going
on I always turn to my trust friend the .Net Reflector!
(http://www.aisto.com/roeder/dotnet/). Sure enough take a look at this
little
method known as System.Exceptio n.GetBaseExcept ion()...

Public Overridable Function GetBaseExceptio n() As Exception
Dim exception1 As Exception = Me.InnerExcepti on
Dim exception2 As Exception = Me
Do While (Not exception1 Is Nothing)
exception2 = exception1
exception1 = exception1.Inne rException
Loop
Return exception2
End Function

Notice anything funny? It does *not* call the innerExceptions 's
GetBaseExceptio n. It just starts drilling down on *it's own* through the
InnerExceptions directly. Why doesn't it call the InnerException' s
GetBaseExceptio n? That wouldn't be recursion exactly, but it would make
more
sense to me. As it stands, overriding GetBaseExceptio n only works when you
are the outer-most exception. If the overriding exception gets wrapped up
as
an inner exception (say for example in an HttpUnhandledEx ception, as in
this
case) all your precious overridden GetBaseExceptio n logic is *out the
window*.

So I have two questions...

1) Why does IIS display the GetBaseExceptio n of the HttpUnhandledEx ception
instead of the InnerException?

2) Why does System.Exceptio n.GetBaseExcept ion drill down on its own
through
the InnerExceptions , instead of honoring the possibly overridden
GetBaseExceptio ns?

Any information, education, guidance, or I-feel-your-pains would be
greatly
appreciated.

John DeHope

Jul 14 '06 #2
Thanks Karl, that information was very helpful. I mised the docs you
referenced. Normally I just read the code docs that you get from an F1 help
search. Are those you quoted from someplace else?

I'm no best practices guru. I just find the exception messages to sometimes
be infuriatingly obscure. And also, when wrapping up multiple ADO providers
into a single simpler api, I don't want to see the provider's error messages
unless I *really* need them. It's not helpful in my mind to have a wrapper to
eliminate the need to know your ado provider, just to have to know your ado
provider when an exception is thrown. Does that sound like a "my team's
exceptions" situation?

A recent example will help illustrate my point. I had a connection string
pointing to the wrong database server. It just so happens that the server
existsed and was running fine, but did not have a DB by the name I had
mentioned in my conn string. So I did not get an exception for the server
being not-there or unavailable, but I did get one for the database I wanted
not being there. Unfortunately the sql server 2000 exception I got didn't say
"no such database 'X'" it said "unable to connect to database 'X'. Logon
fails for user 'Y'". Is it me or does that sound like a permissions problem,
and not a no-such-database problem? Okay that's a rhetorical question. My
real point to telling this story is to illustrate the kind of (in my mind)
problem with keeping up with individual ado provider's exception messages. My
ado wrapper library now has a custom "database not found" exception that is
thrown whenever such a thing happens, leading to no confusion. Is that a
legitimate usage of exception wrapping? Or is that a "my team's exception"?
Jul 17 '06 #3
Well, the entire when to write your own exceptions and when not to is
confusing to me too :) I've seen what are clear abuses in my mind, which I
think is what the guidelines are warning against. I can certainly see the
need to abstract away a provider-type model away from consumers. It'd be a
nightmare for a consumer to have to worry about catching an SqlException,
XmlException, OledbException. ...must better to let them worry about a
ProviderExcepti on.

About your specific example...I think it's legitimate because your code
participated in the exception (by doing some discovery to find out exactly
what the cause was). In poor cases I've seen, exceptions are repacked for
with little additional information. I think you are adding sufficient
meaning to make it worthwhile..

karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"JohnDeHope 3" <Jo*********@di scussions.micro soft.comwrote in message
news:AD******** *************** ***********@mic rosoft.com...
Thanks Karl, that information was very helpful. I mised the docs you
referenced. Normally I just read the code docs that you get from an F1
help
search. Are those you quoted from someplace else?

I'm no best practices guru. I just find the exception messages to
sometimes
be infuriatingly obscure. And also, when wrapping up multiple ADO
providers
into a single simpler api, I don't want to see the provider's error
messages
unless I *really* need them. It's not helpful in my mind to have a wrapper
to
eliminate the need to know your ado provider, just to have to know your
ado
provider when an exception is thrown. Does that sound like a "my team's
exceptions" situation?

A recent example will help illustrate my point. I had a connection string
pointing to the wrong database server. It just so happens that the server
existsed and was running fine, but did not have a DB by the name I had
mentioned in my conn string. So I did not get an exception for the server
being not-there or unavailable, but I did get one for the database I
wanted
not being there. Unfortunately the sql server 2000 exception I got didn't
say
"no such database 'X'" it said "unable to connect to database 'X'. Logon
fails for user 'Y'". Is it me or does that sound like a permissions
problem,
and not a no-such-database problem? Okay that's a rhetorical question. My
real point to telling this story is to illustrate the kind of (in my mind)
problem with keeping up with individual ado provider's exception messages.
My
ado wrapper library now has a custom "database not found" exception that
is
thrown whenever such a thing happens, leading to no confusion. Is that a
legitimate usage of exception wrapping? Or is that a "my team's
exception"?

Jul 17 '06 #4

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

Similar topics

11
2148
by: Grant Edwards | last post by:
I've read over and over that Python leaves floating point issues up to the underlying platform. This seems to be largely true, but not always. My underlying platform (IA32 Linux) correctly handles 1.0/0.0 and 0.0/0.0 according to the IEEE 754 standard, but Python goes out of its way to do the wrong thing. 1/0 is defined by the standard...
3
3486
by: Pierre Rouleau | last post by:
The std::exception class defined in the Standard C++ <exception> header specifies that the constructors could throw any exception becuase they do not have a throw() specification. Why is that? Is this because there could be an exception thrown when the code creates a std::exception? I would assume that is not the case. However, if I want...
13
5029
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
2
3499
by: felecha | last post by:
I learned about Exceptions in School, and now that I'm building my first real application on the job, I'm going through it trying to analyze it for all possible Exceptions so I can handle them effectively. I've been looking in the Help / Index for each Method's documentation, and looking at the listed Exceptions. I'm finding a number of...
2
14248
by: TheBurgerMan | last post by:
Hi all. I am using W2K3, .NET2 on a machine running AD and Exchange. I started getting the message below last week. I googled the error and not much was returned, but I did find this; http://forums.asp.net/918725/ShowPost.aspx I tried both items in the forum and it works for a few hits then it happens again! Anyone have the same...
11
1922
by: benben | last post by:
Recently I have come across a number of struct's that I think is quite interesting: typedef struct whatever_tag { int member1; char member2; // ... char last_member; // interesting line
1
6207
by: AD | last post by:
Hi I am trying to read elements of a vector from console (cin) in two steps ie, read a list of numbers from cin. Sort these. Then read from cin again and append in same vector. /*****************************************************************/ #include<iostream> #include<algorithm> #include<vector>
4
1875
by: fyleow | last post by:
I create a new Python file with the following using Wing IDE. import feedparser d = feedparser.parse("http://feedparser.org/docs/examples/atom10.xml") print d.feed.title I get this error when I debug. AssertionError:
7
1500
by: Bo Yang | last post by:
Hi , I am reading some boost code now , and I got confused by the following code in the add_reference type trait . template <class TT&(* is_reference_helper1(wrap<T>) )(wrap<T>); char is_reference_helper1(...); template <class Tno_type is_reference_helper2(T&(*)(wrap<T>)); yes_type is_reference_helper2(...);
0
7894
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...
0
8172
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. ...
0
8320
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...
1
7929
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...
0
8190
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...
0
6577
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5370
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...
0
3841
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1424
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.