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

Adding to Exception ?

Hi,
I have an exception handler which captures the exception.
Exception e = Server.GetLastError().InnerException;

I want to add an identifying ID (such as Invoice No) to "e". How do I do
that ??
Thanks for your time.

Nov 15 '05 #1
8 1719
exBK <ex**@yahoo.com> wrote:
I have an exception handler which captures the exception.
Exception e = Server.GetLastError().InnerException;

I want to add an identifying ID (such as Invoice No) to "e". How do I do
that ??


The typical thing to do is create your own exception type which
contains the invoice number, and create an exception which contains the
original exception as its "inner exception", then throw that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
Couldn't you just
throw new System.Exception(ID, e);

Then again, you wouldn't know at run time what kind of exception this was.
So ...
Depends on what you need I suppose.

--
The hotmail account will most likely not be read, so please respond only
to the news group.
Nov 15 '05 #3
Thanks for the reply. Any example of how to do it ? I am not sure what you
meant by "exception type" ...

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...
exBK <ex**@yahoo.com> wrote:
I have an exception handler which captures the exception.
Exception e = Server.GetLastError().InnerException;

I want to add an identifying ID (such as Invoice No) to "e". How do I do that ??


The typical thing to do is create your own exception type which
contains the invoice number, and create an exception which contains the
original exception as its "inner exception", then throw that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #4
The below is from MSDN.
http://msdn.microsoft.com/library/de...exceptions.asp

You could add your Property to hold the your invoice number.

using System;
public class EmployeeListNotFoundException: ApplicationException
{
public EmployeeListNotFoundException()
{
}
public EmployeeListNotFoundException(string message)
: base(message)
{
}
public EmployeeListNotFoundException(string message, Exception inner)
: base(message, inner)
{
}
}

Nov 15 '05 #5
You could do this, but the ID would only be part of the message. That means
that if some code wanted to know the ID, they would have to parse the
message. Parsing messages is usually a bad idea as you are then dependent
on the format of the message.

"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:op**************@msnews.microsoft.com...
Couldn't you just
throw new System.Exception(ID, e);

Then again, you wouldn't know at run time what kind of exception this was.
So ...
Depends on what you need I suppose.

--
The hotmail account will most likely not be read, so please respond only
to the news group.

Nov 15 '05 #6
public class NewException : Exception{
public NewException(string message, int id)
: base(message){
__theId = id;
}

public int Id{
get{return __theId;}
set{__theId = value;}
}

private int __theId;
}

"exBK" <ex**@yahoo.com> wrote in message
news:uj**************@TK2MSFTNGP10.phx.gbl...
Thanks for the reply. Any example of how to do it ? I am not sure what you meant by "exception type" ...

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...
exBK <ex**@yahoo.com> wrote:
I have an exception handler which captures the exception.
Exception e = Server.GetLastError().InnerException;

I want to add an identifying ID (such as Invoice No) to "e". How do I do that ??


The typical thing to do is create your own exception type which
contains the invoice number, and create an exception which contains the
original exception as its "inner exception", then throw that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 15 '05 #7
inline
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...
exBK <ex**@yahoo.com> wrote:
I have an exception handler which captures the exception.
Exception e = Server.GetLastError().InnerException;

I want to add an identifying ID (such as Invoice No) to "e". How do I do that ??


The typical thing to do is create your own exception type which
contains the invoice number, and create an exception which contains the
original exception as its "inner exception", then throw that.


Small point. That depends if he is catching some other type in a catch
handler, wrapping it in a new exception (his own custom exception object),
and then thowing the new type from the catch handler, or simply creating a
new exception of this own type anywhere in the code and throwing it.

This is the "catch-wrap-throw" technique, which can only be done from a
catch handler, versus the "throw new type" technique, which can be done
anywhere.

Another small point. His example implies that he is retrieving the exception
from a server. If this is the case then defining a custom type requires that
the assembly that contains the exception type definition be distributed so
that both client and server have access to the same assembly. Otherwise when
the exception is deserialzed on the client it will throw an exception (I
forget which one) which will mask the "real" exception.

His code shows he was accessing the innerexception property to ascertain the
cause. This is almost always a bad idea - he should access the outer
exception for the error reason and examine the inner exception(s) more for
documentation purposes then for programmatic action.

Nov 15 '05 #8
Just a small side note.
You should derive from ApplicationException and not directly from Exception.
This is a kind of "best practice" given by MS

José
"Peter Rilling" <pe***@nospam.rilling.net> wrote in message
news:uX**************@TK2MSFTNGP10.phx.gbl...
public class NewException : Exception{
public NewException(string message, int id)
: base(message){
__theId = id;
}

public int Id{
get{return __theId;}
set{__theId = value;}
}

private int __theId;
}

"exBK" <ex**@yahoo.com> wrote in message
news:uj**************@TK2MSFTNGP10.phx.gbl...
Thanks for the reply. Any example of how to do it ? I am not sure what

you
meant by "exception type" ...

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...
exBK <ex**@yahoo.com> wrote:
> I have an exception handler which captures the exception.
> Exception e = Server.GetLastError().InnerException;
>
> I want to add an identifying ID (such as Invoice No) to "e". How do I
do
> that ??

The typical thing to do is create your own exception type which
contains the invoice number, and create an exception which contains

the original exception as its "inner exception", then throw that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



Nov 15 '05 #9

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

Similar topics

4
by: Rhamphoryncus | last post by:
First a bit about myself. I've been programming in python several years now, and I've got several more years before that with C. I've got a lot of interest in the more theoretical stuff (language...
3
by: Andy_Alpha | last post by:
Here's code,that i think,add user in group .... DirectoryEntry grp=new DirectoryEntry(GroupName); MessageBox.Show(GroupName,"group to add"); try { if (grp!=null) { grp.Invoke("Add",new...
9
by: Greg | last post by:
Binding Manager & dataset - won't add record I've got an untyped dataset with controls bound through code. The user can select a question number from a bound combobox, and the question number and...
26
by: Simon Jefferies | last post by:
Hello, I am trying to add an item to a checked list box, like: clbList.Items.add("Hello",true) I get an error back: Run-time exception thrown: System.ArgumentOutOfRangeException -...
47
by: Pierre Barbier de Reuille | last post by:
Please, note that I am entirely open for every points on this proposal (which I do not dare yet to call PEP). Abstract ======== This proposal suggests to add symbols into Python. Symbols...
2
by: Flack | last post by:
Hey guys, I have a DataGrid and DataTable field in my class : private ImageDataGrid dataGrid1; //ImageDataGrid extends dataGrid and just overides OnMouseDown private DataTable dt = new...
0
by: =?Utf-8?B?QW5keSBZdQ==?= | last post by:
Hi, I'm trying to return exceptions from a WCF Service using FaultExceptions. I got the service compiled and running. But I get an error while adding a service reference to it. The error reads: "...
3
by: =?Utf-8?B?QWxleGFuZGVyIFd5a2Vs?= | last post by:
I recently raninto major problems when I added the Exception namespace to the Project which has my DBML file attached to it. Once I renamed all the Exceptions instances to Syste.Exception the...
4
by: Lewis Holmes | last post by:
Hi I have the following situation in one of my asp.net pages. The user can add multiple table rows to a form by selecting a button. These rows can contain asp.net controls. When this button is...
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$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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.