在构造之后设置Execption消息 [英] Setting Execption messager after construction
问题描述
我需要在已构造的Exception上设置异常消息。
有没有办法实现这个目标?
Psudo代码:
公共类MyException:Exception {
public MyException(Hashtable myData):base(" dummy"){
foreach(myData.Keys中的字符串sTemp){
this.Message + =" ;," + sTemp;
}
}
}
使用这样:
Hashtable myData = new Hashtable();
myData.Add(" car",4);
myData.Add(" truck",6);
抛出新的MyException(myData);
Rasmus写道:I需要在已经构造的异常上设置异常消息。有没有办法实现这个?
System.Exception中的消息字段是内部的,所以没有办法
更改它来自另一个集会。你可以做的一件事是创建一个类
,为你的例外类型生成消息:
class MyExceptionMessageBuilder {
private字符串消息;
public MyExceptionMessageBuilder(Hashtable params){
//从params构建消息
this.message = ...
}
公共字符串消息{
get {return this.message; }
}
干杯,
-
Joerg Jooss
jo ********* @ gmx.net
>
您好,
消息是一个只读属性,您无法更改它,并且您没有
访问成员变量。 />
您可以做的是覆盖Message属性。只需添加一个字符串变量
,用你的哈希表构建并按照描述修改消息。
关于这个的坏处是在一个catch(Exception)你不会得到你的
自定义消息,但Exception'的生成一个,访问你的意志
需要捕获(MyException)
也许有人可以解释为什么MS不允许访问组成Message属性的变量
实例(如果有的话)。
干杯,
-
Ignacio Machin,
ignacio.machin at dot.state.fl.us
佛罗里达州交通局
Rasmus <太阳神**** @ nospam.nospam>在消息中写道
新闻:7A ********************************** @ microsof t.com ...我需要在已构造的Exception上设置异常消息。
有没有办法实现这个目标?
Psudo代码:
public class MyException:Exception {
public MyException(Hashtable myData):base(" dummy" ){
foreach(myData.Keys中的字符串sTemp){
this.Message + ="," + sTemp;
}
}
}
像这样使用:
Hashtable myData = new Hashtable();
myData.Add( car,4);
myData.Add(" truck",6);
抛出新的MyException(myData);
感谢您的帖子。
您能提供代码示例吗?
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);
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
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);
Thanks for the post.
Could you provide a code sample?
这篇关于在构造之后设置Execption消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!