|
|
|
Hi All,
Can anyone please tell me hoe to set the value to a custom exception object?
If so, pls give a code snippet?
thanks in advance
Regards
babu
|
|
|
|
|
What value?
You can create an Exception object with any message you like:
new ApplicationException("Nu sket det sig rejält, må jag säga.")
You can also create specific Exception objects for different kinds of errors. Some examples:
new ArgumentNullException("path", "The path argument may not be null.")<br />
new NullReferenceException("The operation resulted in a null reference.")<br />
new IndexOutOfRangeException("The position has to be in the range 0 to 9.")<br />
---
It's amazing to see how much work some people will go through just to avoid a little bit of work.
|
|
|
|
|
but for this i've to set the value to the property in the class as follows.
this.Message = "msg"; or exception.Message="msg";
how to set that??
|
|
|
|
|
Set the message property of the exception in the constructor.
|
|
|
|
|
yes. as you said i put the message property in constructor. even thne it shows the error i've mentioned in prev message.
babu
|
|
|
|
|
babutkchn wrote: as you said i put the message property in constructor. even thne it shows the error i've mentioned in prev message
Did you call it right:
public MyException : ApplicationException<br />
{<br />
public MyException(string message) : base (message)<br />
{<br />
}<br />
}
|
|
|
|
|
|
Then the exception should have the message that is passed in to the constructor. Which is as it should be.
|
|
|
|
|
I explained how you do that in the other thread. If you didn't create mutiple threads about the same thing, the confusion would be less.
---
It's amazing to see how much work some people will go through just to avoid a little bit of work.
|
|
|
|
|
for example in the following code i've set the value to both source and message.but the application shows error for the MEssage property. I need the soution for this.i need to set the value as in the code. how to set that? Kindly tell me?
public class CustomExp : ApplicationException
{
public void cust(String msg)
{
this.Source = "test";
this.Message = "testing";
}
}
babu
|
|
|
|
|
Set the value of message in the constructor, as in the examples given in the other thread you started.
|
|
|
|
|
I explained how you do that in the other thread. If you didn't create mutiple threads about the same thing, the confusion would be less.
---
It's amazing to see how much work some people will go through just to avoid a little bit of work.
|
|
|
|
|
Hi all,
how to set a value to custom exception message? I tried to set the value, since its a read only property couldnt able to set it...i tried it as follows
public class Customexp:ApplicationException
{
public void frm(String mess)
{
this.Message="test";
this.Source = "kjashdfkj";
}
}
for this code, error is as follows
Property or indexer 'System.Exception.Message' cannot be assigned to -- it is read only
can anyone tell me how to solve this problem.
the clear point is, i've to create an exception object and have to set the value for it.
Regards
babu
|
|
|
|
|
Hello babu,
This properties are virtual, which you can override in youre class.
public override string Message
{
get "test";
}
public override string Source
{
get "test";
}
Hope that helps!
All the best,
Martin
|
|
|
|
|
That won't even compile.
|
|
|
|
|
Sorry,
public override string Message
{
get
{
return "test";
}
}
public override string Source
{
get
{
return "test";
}
}
Martin
|
|
|
|
|
You set the message in the constructor by using the message in the constructor for the base class:
public CustomExp() : base("The message.") {
this.Source = "The source.";
}
---
It's amazing to see how much work some people will go through just to avoid a little bit of work.
|
|
|
|
|
Hello Guffa,
Isn't Source also ReadOnly (Only get accessor)?
All the best,
Martin
|
|
|
|
|
Martin# wrote: Isn't Source also ReadOnly (Only get accessor)?
Nope.
MSDN Library: Exception.Source property[^]
---
It's amazing to see how much work some people will go through just to avoid a little bit of work.
|
|
|
|
|
Ok, Thats really not my day.
Sorry for asking!
|
|
|
|
|
thank you for your kind reply.The this.source is working. i need to set the value to this.message. can you tell me for that
babu
|
|
|
|
|
I already told you how to set the message. Read my previous post.
If you don't understand it, explain what it is that you don't understand.
---
It's amazing to see how much work some people will go through just to avoid a little bit of work.
|
|
|
|
|
If you don't need to do any manipulation of the message (and this is generally not a good idea), you should set Message by calling the base constructor:
public class Customexp: ApplicationException
{
public Customexp(string message) : base (message) {}
}
the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
Deja View - the feeling that you've seen this post before.
|
|
|
|