471,327 Members | 2,682 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Exception handling in property.

Hi all,

I am having c# application containing one property.

What i want is not to allow user to set null value to property.

So if in code user tries to set value as null i want to through
exception.

Can i do this, if yes then please tell me the way of throwing exception
in property and catching it in code where property is set.

Any help will be truely apprecaited.

Thanks in advnace.

Feb 6 '06 #1
1 1625
archana <tr**************@yahoo.com> wrote:
I am having c# application containing one property.

What i want is not to allow user to set null value to property.

So if in code user tries to set value as null i want to through
exception.

Can i do this, if yes then please tell me the way of throwing exception
in property and catching it in code where property is set.

Any help will be truely apprecaited.


You'd throw the exception in the same way you would from a method:

if (value==null)
{
throw new WhateverException();
}

Now, in terms of catching it - why would you catch it in the code where
the property is set? If you were going to catch it at that level, you'd
be far better off testing for nullity yourself first. You should almost
certainly be catching it much higher up the call stack - and you catch
an exception thrown by a property in the same way as you catch any
other exception. There's really no difference.

--
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
Feb 6 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Serge Calderara | last post: by
7 posts views Thread by RSB | last post: by
5 posts views Thread by juergen perlinger | last post: by
6 posts views Thread by Vadivel Kumar | last post: by
2 posts views Thread by nunya5200-bidness | last post: by
4 posts views Thread by Peter Aitken | last post: by
4 posts views Thread by ZorpiedoMan | last post: by
7 posts views Thread by ZorpiedoMan | last post: by
1 post views Thread by Efi Merdler | 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.