Answered Error Handling and Classes

cossie

Member
Joined
Jul 3, 2008
Messages
6
Programming Experience
1-3
Hi,

I've added a class to my application - I'm learning VB.NET as I go - and I'm wondering about the best way to handle errors and at what level should I be dealing with them at.

At the moment, I let the try / catch blocks handle any exceptions that may occur. But say if I have stuff like an XNameSpace as a property in a class and the parameter supplied to it turns out to be incorrect, which I wouldn't find out until calling:

VB.NET:
dim ns = XNameSpace.Get(nsURL)
I'm confused as to how I should handle errors of that sort (not the specific exception now just in general software terms). Should I check the validity of it in the constructor and throw an exception there or should I leave it up to the calling program to try/catch the exception?

Throwing an exception seems useful because if I was to return an error methods would have to be changed to constantly return an error type (an integer or something).

Any general tips or advice on error handling would be great, I'm currently reading through the Exception and Error Handling articles here: http://msdn.microsoft.com/en-us/library/s6da8809(VS.80).aspx but it's not really answering my question or I'm just not understanding the information.

Thanks,

Cossie
 
Last edited:

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,587
Location
Norway
Programming Experience
10+
I don't understand your example, but validating property values should be done in the property setter, if value given is not valid you must throw an exception. Example:
VB.NET:
Private newPropertyValue As Integer
Public Property NewProperty() As Integer
    Get
        Return newPropertyValue
    End Get
    Set(ByVal value As Integer)
        If value < 0 Then
            Throw New ArgumentOutOfRangeException("Value must be positive.")
        End If
        newPropertyValue = value
    End Set
End Property
The same applies for validation of parameter values of constructors, but if these just set a property value you don't need to add validation logic twice, using the property instead of the private field will benefit from the present validation in property setter.

Ask again and explain further if this was not what you had in mind.
 

cossie

Member
Joined
Jul 3, 2008
Messages
6
Programming Experience
1-3
Thank you very much JohnH that answers most of what I was asking (regarding constructors). After re-reading my post I realise I didn't explain myself well at all, sorry.

I think a better way of explaining it is, if I have a method in a class that could result in an error, how should I deal with that?

I have a class called MailReader, I create an instance of MailReader in my main class.
VB.NET:
Dim URL as String
...
...
Dim mr as new MailReader(URL) 
...

I then in the main class call a method from MailReader, called Init().
VB.NET:
try 
  mr.Init()
catch
...
end try

Init() contains
VB.NET:
ns = XNameSpace.Get(URL)
if URL happens to be incorrect (which will result in an error), should I deal with that error in Init() or leave it to the main class's try/catch to deal with the error?
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,587
Location
Norway
Programming Experience
10+
I don't understand your deferment, Init suggest it is a initialization method, initializing a class is also something you do in the constructor method. On top of that, it looks like you here somehow are referring to the Url value that was submitted to the constructor parameter. "ns = XNameSpace.Get(URL)" - what does that mean? As your Init method has no parameters, what purpose does it have as a standalone method, and why does it depend on external values? It look to me you are making this more complicated than it need be, and by doing that introduce new synchronization problems.

In general a method should throw an exception if it can't continue with given parameter values or fulfill its purpose, unless it is a function method and the return value give room for returning valid results for "unsuccessful" operation.
 

cossie

Member
Joined
Jul 3, 2008
Messages
6
Programming Experience
1-3
I don't understand your deferment, Init suggest it is a initialization method, initializing a class is also something you do in the constructor method. On top of that, it looks like you here somehow are referring to the Url value that was submitted to the constructor parameter. "ns = XNameSpace.Get(URL)" - what does that mean?

I have an awful lot of code and don't really want to post it here - it would be very messy to try to format for starters, and i tried to trim things down to be reasonable in a post. I posted a bad example and a poor explanation. Sorry :eek:

As your Init method has no parameters, what purpose does it have as a standalone method, and why does it depend on external values? It look to me you are making this more complicated than it need me, and by doing that introduce new synchronization problems.

Okay Init() was a bad name choice, I should have used something like Setup() instead. I took some of those things out of the constructor because I was unsure at the time about how to deal with the error that might occur.

In general a method should throw an exception if it can't continue with given parameter values or fulfill its purpose, unless it is a function method and the return value give room for returning valid results for "unsuccessful" operation.

Thanks, i understand that now :)
 
Top Bottom