A
Andy B.
I have a class called Address. I need to catch exceptions from it if the
values for the properties are wrong. This is how they work:
1. Address.Type as string. Throws InvalidOperationException if not set.
2. Address.Street as string. Throws InvalidOperationException if not set.
3. Address.Line1. Has no errors since it isn't required.
4. Address.City. Throws InvalidOperationException if not set.
5. Address.State. Throws ArgumentOutOfRangeException if value is < or > than
2 characters long and ArgumentNullException if set to Nothing.
6. Address.ZipCode. Throws InvalidOperationException if not set.
7. Address.Country. See #6.
my questions:
1. Is this too many exceptions being thrown for a single class? If so, how
do you do error handling?
2. If the exceptions are ok, how do you tell the user when each one of them
changes?
'I tried this and only got the Address.State ArgumentOutOfRangeException.
Any idea what the problem is?
try
dim TestAddress as new Address()
TestAddress.State="cal" 'Get ArgumentOutOfRangeException here.
TestAddress.Street = Nothing 'Get InvalidOperationException here.
TestAddress.City = "" 'Get InvalidOperationException here.
catch ex as ArgumentOutOfRangeException
messagebox.show(ex.message)
catch ex as InvalidOperationException
messagebox.show(ex.messabe)
end try
values for the properties are wrong. This is how they work:
1. Address.Type as string. Throws InvalidOperationException if not set.
2. Address.Street as string. Throws InvalidOperationException if not set.
3. Address.Line1. Has no errors since it isn't required.
4. Address.City. Throws InvalidOperationException if not set.
5. Address.State. Throws ArgumentOutOfRangeException if value is < or > than
2 characters long and ArgumentNullException if set to Nothing.
6. Address.ZipCode. Throws InvalidOperationException if not set.
7. Address.Country. See #6.
my questions:
1. Is this too many exceptions being thrown for a single class? If so, how
do you do error handling?
2. If the exceptions are ok, how do you tell the user when each one of them
changes?
'I tried this and only got the Address.State ArgumentOutOfRangeException.
Any idea what the problem is?
try
dim TestAddress as new Address()
TestAddress.State="cal" 'Get ArgumentOutOfRangeException here.
TestAddress.Street = Nothing 'Get InvalidOperationException here.
TestAddress.City = "" 'Get InvalidOperationException here.
catch ex as ArgumentOutOfRangeException
messagebox.show(ex.message)
catch ex as InvalidOperationException
messagebox.show(ex.messabe)
end try