471,319 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

overriding exception message

I wanted to create an exception handler for certain processing I do - but
although I can 'throw' to the constructor with a parameter, I cannot
override the exception message property, the error says its read only. How
can or if a person can create the override ?

class privateException: Exception
{
public privateException(string str)
{
Console.WriteLine("User Defined Exception" +
this.Message+this.StackTrace);
// this.Message=" Special Error " + str
// you cannot seem to create modify the message.
// this input could cause some additional interaction - maybe here
// would be where a file could be written
}
public privateException()
{

}
}
--
Andrew
Jan 27 '06 #1
4 15121
> I wanted to create an exception handler for certain processing I do -
but although I can 'throw' to the constructor with a parameter, I
cannot override the exception message property, the error says its
read only. How can or if a person can create the override ?

class privateException: Exception
{
public privateException(string str)
{
Console.WriteLine("User Defined Exception" +
this.Message+this.StackTrace);
// this.Message=" Special Error " + str
// you cannot seem to create modify the message.
// this input could cause some additional interaction -
maybe here
// would be where a file could be written
}
public privateException()
{
}
}

class PrivateException : ApplicationException
{
public PrivateException()
{}

public PrivateException(string message) : base(message)
{}

public PrivateException(string message, Exception innerException) : base(message,
innerException)
{}
}
Jan 27 '06 #2
andrewcw <an******@acw.com> wrote:
I wanted to create an exception handler for certain processing I do - but
although I can 'throw' to the constructor with a parameter, I cannot
override the exception message property, the error says its read only. How
can or if a person can create the override ?

class privateException: Exception
{
public privateException(string str)
{
Console.WriteLine("User Defined Exception" +
this.Message+this.StackTrace);
// this.Message=" Special Error " + str
// you cannot seem to create modify the message.
// this input could cause some additional interaction - maybe here
// would be where a file could be written
}
public privateException()
{

}
}


Well, you could override the property:

class PrivateException : Exception
{
string otherMessage;

public PrivateException (string message)
{
otherMessage = message;
}

public override string Message
{
get { return otherMessage; }
}
}

However, you might as well pass the message to the base constructor in
the first place:

class PrivateException : Exception
{
public PrivateException (string message) : base (message)
{
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 27 '06 #3
Thankyou ! I really appreciate the help !
--
Andrew
"Jon Skeet [C# MVP]" wrote:
andrewcw <an******@acw.com> wrote:
I wanted to create an exception handler for certain processing I do - but
although I can 'throw' to the constructor with a parameter, I cannot
override the exception message property, the error says its read only. How
can or if a person can create the override ?

class privateException: Exception
{
public privateException(string str)
{
Console.WriteLine("User Defined Exception" +
this.Message+this.StackTrace);
// this.Message=" Special Error " + str
// you cannot seem to create modify the message.
// this input could cause some additional interaction - maybe here
// would be where a file could be written
}
public privateException()
{

}
}


Well, you could override the property:

class PrivateException : Exception
{
string otherMessage;

public PrivateException (string message)
{
otherMessage = message;
}

public override string Message
{
get { return otherMessage; }
}
}

However, you might as well pass the message to the base constructor in
the first place:

class PrivateException : Exception
{
public PrivateException (string message) : base (message)
{
}
}

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

Jan 28 '06 #4
Thanks ! I really appreciate the help !
--
Andrew
"chris martin" wrote:
I wanted to create an exception handler for certain processing I do -
but although I can 'throw' to the constructor with a parameter, I
cannot override the exception message property, the error says its
read only. How can or if a person can create the override ?

class privateException: Exception
{
public privateException(string str)
{
Console.WriteLine("User Defined Exception" +
this.Message+this.StackTrace);
// this.Message=" Special Error " + str
// you cannot seem to create modify the message.
// this input could cause some additional interaction -
maybe here
// would be where a file could be written
}
public privateException()
{
}
}

class PrivateException : ApplicationException
{
public PrivateException()
{}

public PrivateException(string message) : base(message)
{}

public PrivateException(string message, Exception innerException) : base(message,
innerException)
{}
}

Jan 28 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Chris Leffer | last post: by
1 post views Thread by iana_kosio | last post: by
10 posts views Thread by WhiskyRomeo | last post: by
11 posts views Thread by Russ P. | last post: by
reply views Thread by WaterWalk | last post: by
reply views Thread by rosydwin | last post: by

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.