Setting Execption messager after construction

G

Guest

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);
 
J

Joerg Jooss

Rasmus said:
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,
 
I

Ignacio Machin \( .NET/ C# MVP \)

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,
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi

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

cheers,
 
G

Guest

Hi,

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

Zürcher See

try{ ... }
catch(System.Exception e)
{
throw new System.Exception("This is my error message",e);
}
 
K

Kevin Yu [MSFT]

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."
 
J

Joerg Jooss

Rasmus said:
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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top