473,404 Members | 2,137 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,404 software developers and data experts.

Setting Execption messager after construction

I need to set the exception message on an already constructed Exception. Is
there a way to accomplish this?

Psudo Code:
public class MyException : Exception {
public MyException (Hashtable myData) : base ("dummy") {
foreach (string sTemp in myData.Keys){
this.Message += "," + sTemp;
}
}
}
USE LIKE THIS :
Hashtable myData = new Hashtable();
myData.Add("car",4);
myData.Add("truck",6);
throw new MyException(myData);

Nov 16 '05 #1
9 1458
Rasmus wrote:
I need to set the exception message on an already constructed
Exception. Is there a way to accomplish this?


The message field in System.Exception is internal, so there's no way
changing it from another assembly. One thing you can do is create a class
that produces the messages for your exception type:

class MyExceptionMessageBuilder {
private string message;
public MyExceptionMessageBuilder(Hashtable params) {
// Build message from params
this.message = ...
}

public string Message {
get { return this.message; }
}

Cheers,
--
Joerg Jooss
jo*********@gmx.net
Nov 16 '05 #2
Hi,

Message is a read only property, you cannot change it, and you dont have
access to the member variable.
What you can do is override Message property. just add a string variable
that you build with your hashtable and modify Message as described.

The bad thing about this is in a catch(Exception ) you will not get your
custom message, but the Exception's generated one, to access your youwill
need to catch ( MyException )

Maybe somebody can explain why MS does not allow access to the variable
instance ( if any exist ) that make up Message property.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Rasmus" <Ra****@nospam.nospam> wrote in message
news:7A**********************************@microsof t.com...
I need to set the exception message on an already constructed Exception. Is there a way to accomplish this?

Psudo Code:
public class MyException : Exception {
public MyException (Hashtable myData) : base ("dummy") {
foreach (string sTemp in myData.Keys){
this.Message += "," + sTemp;
}
}
}
USE LIKE THIS :
Hashtable myData = new Hashtable();
myData.Add("car",4);
myData.Add("truck",6);
throw new MyException(myData);

Nov 16 '05 #3
Thanks for the post.

Could you provide a code sample?
Nov 16 '05 #4
Hi

Take a look at Jooss's post, it has the code there :)

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Rasmus" <Ra****@nospam.nospam> wrote in message
news:49**********************************@microsof t.com...
Thanks for the post.

Could you provide a code sample?

Nov 16 '05 #5
Hi,

The reason i asked for a code sample is that I didn't quite understand the
sample from Jooss...
Nov 16 '05 #6
try{ ... }
catch(System.Exception e)
{
throw new System.Exception("This is my error message",e);
}

"Rasmus" <Ra****@nospam.nospam> schrieb im Newsbeitrag
news:FA**********************************@microsof t.com...
Hi,

The reason i asked for a code sample is that I didn't quite understand the
sample from Jooss...

Nov 16 '05 #7
But you are setting the message in the constructor. Have you read the tread?

Nov 16 '05 #8
Hi Rasmus,

class MyExceptionMessageBuilder {
private string message; //create your own message member so
you can modify it.
public MyExceptionMessageBuilder(Hashtable params) {
// Build message from params
this.message = ...
}

public string Message {
get { return this.message; }
}

public void SetMessage(string msg){ //I think you can also add this method
to set message for your own exception.
this.message = .....................
}

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 16 '05 #9
Rasmus wrote:
But how does this relate to throwing a new execption?
Instead of implicitly constructing the message, just do it explicitly.
It seems that this is code is just a class helping me to store a
string...


It builds a message specifically for your exception.

So instead of

throw new MyException(params);

you use

string message = new MyExceptionMessageBuilder(params).Message;
throw new MyException(message);

Cheers,
--
Joerg Jooss
jo*********@gmx.net

Nov 16 '05 #10

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

Similar topics

1
by: relaxedrob | last post by:
Howdy All! I am really stuck with this one - I want to completely create a table within JavaScript and insert it into the document, with onMouseOut and onMouseOver handlers in the table rows. ...
6
by: Ganesan selvaraj | last post by:
I using C# .net. i want to split the text files based of the some condition. my source text file size may be 4 kb to 4 gb. some time when i split the i got the "out of memory exception. when i...
6
by: Ganesan selvaraj | last post by:
I using C# .net. i want to split the text files based of the some condition. my source text file size may be 4 kb to 4 gb. some time when i split the i got the "out of memory exception. when i...
3
by: Patient Guy | last post by:
Subject line would seem to say it all: How does one trigger the execution of a method within an object or any other code/function with the setting of an object property? More elaboration for...
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: 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
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...
0
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...
0
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...
0
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,...
0
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...

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.