473,881 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Proper use of inner exceptions

Hi group,

I am quite new to exceptions in .NET and I am a bit confused of how to
use the inner exceptions. Could anyone explain? Let's say I have a
function that takes a double (X) that is not supposed to be less or
equal to zero. Let's call this function DoSomething.

Also let's say that this function in turn calls another function doing
something with X.

Then if there is a bug in the DoSomething function that causes the
other function to throw an ArgumentOutOfRa ngeException.

void DoSomething(dou ble X)
{
if(X<=0)
throw new ArgumentOutOfRa ngeException("X ","X has to be greater
than zero");
X-=10; // This is a bug..
AnotherFunction (X);
}

void AnotherFunction (double X)
{
if(X<=0)
throw new ArgumentOutOfRa ngeException("X "," X has to be
greater than zero ");
Jul 21 '05 #1
7 1686
Kenny,
Ok, now if I call DoSomething with 10 or less I will get an
ArgumentOutOfR angeException. Then I will think that this is because I
called DoSomething with a value of X less or equal to zero.


Not if you examine the exception's StackTrace, it will tell you that
it originated from AnotherFunction .

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jul 21 '05 #2
IMO :
- there is no need to try to catch this situation, just correct the bug
- if DoSomething requires x<=0 because it calls AnotherFunction that
requires x<=0, don't catch the exception in DoSomething. IMO the exception
just be thrown exactly where your requirement is no met.
- an exception is to catch programming or environment error. Here I would
validate the user input before calling the function.

Patrice
--

"Kenny Cutter" <st***@cncsimul ator.com> a écrit dans le message de
news:83******** *************** ***@posting.goo gle.com...
Hi group,

I am quite new to exceptions in .NET and I am a bit confused of how to
use the inner exceptions. Could anyone explain? Let's say I have a
function that takes a double (X) that is not supposed to be less or
equal to zero. Let's call this function DoSomething.

Also let's say that this function in turn calls another function doing
something with X.

Then if there is a bug in the DoSomething function that causes the
other function to throw an ArgumentOutOfRa ngeException.

void DoSomething(dou ble X)
{
if(X<=0)
throw new ArgumentOutOfRa ngeException("X ","X has to be greater
than zero");
X-=10; // This is a bug..
AnotherFunction (X);
}

void AnotherFunction (double X)
{
if(X<=0)
throw new ArgumentOutOfRa ngeException("X "," X has to be
greater than zero ");
.
.
// Do something with X
}

Ok, now if I call DoSomething with 10 or less I will get an
ArgumentOutOfRa ngeException. Then I will think that this is because I
called DoSomething with a value of X less or equal to zero.

try
{
double x= getValueFromUse r();
DoSomething(X);
}
catch(ArgumentO utOfRangeExcept ion e)
{
MessageBox.Show ("Dear User, please enter a value greater than 0");
}

But the problem here was not the input from the user but the bug in
DoSomething...

So what is the correct use of exceptions/innerexceptions ? Should I
instead change the DoSomething to catch exceptions and rethrow them as
inner exceptions in an own exception type? Like this?

void DoSomething(dou ble X)
{
if(X<=0)
throw new ArgumentOutOfRa ngeException("X ","X is to small");
X-=10; // This is a bug..
try
{
AnotherFunction (X);
}
catch(ArgumentO utOfRangeExcept ion e)
{
MyOwnException myex = new MyOwnException( "Call to
AnotherFunction failed",e); // Set innerexception as e
}
}

or should I catch all exceptions that AnotherFunction could throw
(catch the exceptions base type) and rethrow them as my own exception
with the innerexception set to the original exception?

Does this mean that in all assemblies that I write, in all public
functions, I should catch all exceptions and rethrow them as my own
exceptions with the innerexceptions set to the original exception? Or
have I missed something here, is there another approach to avoid the
problem above?

Any help to make me see this clearer is highly appreciated.

/kenny

Jul 21 '05 #3
Hello Kenny,

There have been a few online articles and blogs on "best practices" in .Net
exception handling, so I'd suggest you start there.
This topic has been discussed here many times before.

In a nutshell:
a) catch the error as close to the point where it triggered as possible,
b) log the event
c) respond appropriately so no system state is corrupted or memory is lost,
d) then, if the downstream error means that your current method cannot
perform its responsibilitie s in a way that gracefully handles the error, you
return a new exception containing the old one as an inner exeption.

80% of the time, you will not create a new exception to contain the old one.
Also, it is perfectly OK to throw a new ApplicationExce ption() containing
your embedded exception... you don't need to create a custom exception class
for every possible exception. (If you have an entire "class" of exceptions,
then you can benefit from the custom exception type).

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Kenny Cutter" <st***@cncsimul ator.com> wrote in message
news:83******** *************** ***@posting.goo gle.com...
Hi group,

I am quite new to exceptions in .NET and I am a bit confused of how to
use the inner exceptions. Could anyone explain? Let's say I have a
function that takes a double (X) that is not supposed to be less or
equal to zero. Let's call this function DoSomething.

Also let's say that this function in turn calls another function doing
something with X.

Then if there is a bug in the DoSomething function that causes the
other function to throw an ArgumentOutOfRa ngeException.

void DoSomething(dou ble X)
{
if(X<=0)
throw new ArgumentOutOfRa ngeException("X ","X has to be greater
than zero");
X-=10; // This is a bug..
AnotherFunction (X);
}

void AnotherFunction (double X)
{
if(X<=0)
throw new ArgumentOutOfRa ngeException("X "," X has to be
greater than zero ");
.
.
// Do something with X
}

Ok, now if I call DoSomething with 10 or less I will get an
ArgumentOutOfRa ngeException. Then I will think that this is because I
called DoSomething with a value of X less or equal to zero.

try
{
double x= getValueFromUse r();
DoSomething(X);
}
catch(ArgumentO utOfRangeExcept ion e)
{
MessageBox.Show ("Dear User, please enter a value greater than 0");
}

But the problem here was not the input from the user but the bug in
DoSomething...

So what is the correct use of exceptions/innerexceptions ? Should I
instead change the DoSomething to catch exceptions and rethrow them as
inner exceptions in an own exception type? Like this?

void DoSomething(dou ble X)
{
if(X<=0)
throw new ArgumentOutOfRa ngeException("X ","X is to small");
X-=10; // This is a bug..
try
{
AnotherFunction (X);
}
catch(ArgumentO utOfRangeExcept ion e)
{
MyOwnException myex = new MyOwnException( "Call to
AnotherFunction failed",e); // Set innerexception as e
}
}

or should I catch all exceptions that AnotherFunction could throw
(catch the exceptions base type) and rethrow them as my own exception
with the innerexception set to the original exception?

Does this mean that in all assemblies that I write, in all public
functions, I should catch all exceptions and rethrow them as my own
exceptions with the innerexceptions set to the original exception? Or
have I missed something here, is there another approach to avoid the
problem above?

Any help to make me see this clearer is highly appreciated.

/kenny

Jul 21 '05 #4
Kenny,

Nick Malik is right: there are a lot of 'blog articles, online
whitepapers, and even books on the subject of properly handling
exceptions. Here are a few places to start:

http://msdn.microsoft.com/library/de...rp08162001.asp

http://msdn.microsoft.com/msdnmag/is...T/default.aspx

http://msdn.microsoft.com/library/de...ceptdotnet.asp

However, all of this won't stop me from adding my two cents' worth. :)
The philosophy you stated in your opening question is along the right
lines. To my thinking, the purpose of inner exceptions is to transform
exceptions that you know may happen in your method into terms that make
sense to callers.

For example, if you were to require that your argument X be non-zero,
because your method is going to try to divide by X, it would be very
bad form to simply let the method throw a DivideByZeroExc eption. From
the caller's point of view, this is meaningless. The caller has to
understand the inner workings of your method in order to interpret the
exception.

Now, whether you choose to test for X == 0 and throw an
InvalidArgument Exception, or catch the DivideByZeroExc eption and wrap
it in an InvalidArgument Exception is up to you. Most people would do
the former, but you could do the latter if the information about where
the divide by zero happened was of interest and you didn't want to lose
it.

In cases in which you call other methods that throw exceptions, you may
have no choice but to catch them, and then you may wish to transform
them into terms more meaningful to your callers. In these cases you
almost certainly want to wrap them as inner exceptions.

Jul 21 '05 #5
Not to put too fine a point on it:
a double divided by a double won't throw a DivideByZeroExc eption...

Jul 21 '05 #6
Ola
Hi.

I have the same kind of questions as Kenny.
Anyone got some more useful links to help me learn how to properly
handle exceptions.
A link to a good discussion in the subject would be nice.
Jul 21 '05 #7
There have been a few online articles and blogs on "best practices" in
.Net
exception handling, so I'd suggest you start there.
This topic has been discussed here many times before.

In a nutshell:
a) catch the error as close to the point where it triggered as possible,
b) log the event
Usually a good idea, but this may not be the correct action to take - it
depends on the system. The issue of where logging should occur needs to
account for intermediate routines that catch-wrap-throw. You would not want
intermediate catch blocks to also log the exception - it would quickly
overwhelm the logging system with redundant data. I prefer to log it once at
the original exception site, and then log it again at a boundary across
which the exception will propagate. I usually define the boundary as the
edge of a web service, process or appdomain.
c) respond appropriately so no system state is corrupted or memory is
lost,
Easier said then done. Implementing complete rollback semantics across all
objects, external data stores, etc. is non-trivial - in some cases it may
not even be possible. These potential corruption points should be defined
and known.
d) then, if the downstream error means that your current method cannot
perform its responsibilitie s in a way that gracefully handles the error,
you
return a new exception containing the old one as an inner exeption.

80% of the time, you will not create a new exception to contain the old
one.
Also, it is perfectly OK to throw a new ApplicationExce ption() containing
your embedded exception... you don't need to create a custom exception
class
for every possible exception.
As an alternative to using the generic ApplicationExce ption you can throw
the same exception type as the that was caught (clone it). Also, the use of
the ApplicationExce ption type is being discouraged by new MSFT guidelines.

There has also been discussions around the distinction between technical
exceptions and business logic exceptions. Typical usage is for low-level
code to either handle faults or propagate technical exceptions upwards, and
higher level logic then provides a translation layer that maps these into
business logic exceptions that are more understandable to users. In other
words, presenting a user with a null reference exception doesn't help them
much - telling them to enter a valid user name does.
(If you have an entire "class" of exceptions,
then you can benefit from the custom exception type).
There are downsides, mainly with versioning and system evolution, and
propagating exceptions across process boundaries. I would discourage
allowing a custom exception to propagate outside of a web service - the
client may not be able to deserialize it. This may not be an issue for
small, self-contained systems, but for distributed systems, either using web
services, remoting, or multiple appdomains, it can be a problem.


--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Jul 21 '05 #8

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

Similar topics

5
1683
by: Pekka Niiranen | last post by:
Hi, I have Perl code looping thru lines in the file: line: while (<INFILE>) { ... $_ = do something ... if (/#START/) { # Start inner loop
7
1747
by: Kenny Cutter | last post by:
Hi group, I am quite new to exceptions in .NET and I am a bit confused of how to use the inner exceptions. Could anyone explain? Let's say I have a function that takes a double (X) that is not supposed to be less or equal to zero. Let's call this function DoSomething. Also let's say that this function in turn calls another function doing something with X.
2
1112
by: a_newcomb | last post by:
I am using a HttpWebRequest object to a custom webhandler. When processing the request on the server side, I need to return exceptions to the web request. Whenever the exceptions are thrown, web request always receivesSystem.Net.WebException: The remote server returned an error: (500) Internal Server Error. Is it possible to return the actual thrown exception, or to have the WebException contain the inner exception that was initially...
2
2475
by: Sue | last post by:
I've got a simple Application_OnError routine set up as a generic error trap for web applications running on IIS server (VS 2003) that logs error info and the text of the first inner exception. I've had exceptions thrown on occasion that have up to three additional levels of inner exceptions. Is there a way to drill down through each of these inner exceptions (using VB.NET) to capture and report their message? The closest example I can find...
4
1463
by: CK | last post by:
Good Morning, I have a person table with personID. I have a personRate table with personID, rateID, and effectiveDate. I need to select fields from personRate, but I want the fields from the proper record. I need the one child record that has the most current date of the largest rateID. For example a person may have many rate records. I need the record that has
6
1722
by: Lance | last post by:
hi all, using the following code, i never get a message box showing the error, but the default error sound is produced (like there should be an accompanying messagebox). \\\\\\\\\\ Dim FI As FileInfo = _ My.Computer.FileSystem.GetFileInfo(txtSelectedDocument.Text) Try My.Computer.FileSystem.CopyDirectory(FI.DirectoryName, _
14
1302
by: John Salerno | last post by:
Hi guys. I was wondering if someone could suggest some possible structures for an "Education" table in a database. It will contain at minimum university names, years of attendance, and degrees earned. My problem is that I don't know quite how to set this up for people who have attended multiple universities. I could do: university text DEFAULT NULL, yearStart integer DEFAULT NULL, yearEnd integer DEFAULT NULL, degreesEarned text...
5
3801
by: Thomas Guettler | last post by:
Hi, How can you get the traceback of the inner exception? try: try: import does_not_exit except ImportError: raise Exception("something wrong") except:
1
1523
nurikoAnna
by: nurikoAnna | last post by:
how to properly separate sql codes........ I am using &_ as a separator...because it will not suits the visual basic code form...it will skip to the next line that's why I am having trouble in it Below is my code...please help me separate this using the proper separator and proper format if CESDBConn.execute ("SELECT `t_application`.`PersonalInfoID`,`t_application`.` DateOfApplication`,`t_application`.`SchoolLastAtte...
0
9926
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
9775
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11094
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
10715
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...
0
10396
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
7952
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
7105
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
4597
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
3
3221
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.