Hello. Some questions please.
I created a class that contains a read method and an update method on a file. The first one returns an oledbdatareader (with all found rows) and the last one returns an integer (rows affected).
My questions is: what should I return for each classes if there was an exception, what is the correct way to do that
Thanks...

Questions on a class
Coach24
Depending upon you exception handling - you might not need to concern yourself about the return value as this may be rather irrelevent.
Example
Module Module1
Sub Main()
Try
Dim x As New Foo
Dim i As Integer = x.method
MsgBox("This will never be reached - because of the exception handling")
Catch ex As Exception
MsgBox("Exception was thrown" & ex.Message)
End Try
End Sub
End Module
Public Class Foo
Function method() As Integer
Throw New Exception("Exception In Foo")
Return 10
End Function
End Class
If you code step the code - you will notice that the return type from method is irrelevent as the exception thrown will not be handled in the method and passed to the caller which will immediately get focus in the exception catch block. So no return value was set and if you look at the value of i it would still be set to a default value.
You should have some top level exception handler in you code which will avoid any unhandled exception - these account for exceptional situations not encountered.
The following is a slight variation where I can determine an exception and set a default value of -1 which is not the default value of the integer type.
Module Module1
Sub Main()
Dim i As Integer = -1 '//Default Value
Try
Dim x As New Foo
i = x.method()
MsgBox("This will never be reached - because of the exception handling")
Catch ex As Exception
MsgBox("Exception was thrown" & ex.Message)
End Try
MsgBox("Value of i (if i = -1 then still default)" & i.ToString)
End Sub
End Module
Public Class Foo
Function method() As Integer
Throw New Exception("Exception In Foo")
Return -10
End Function
End Class
The simple thing is if the return value is important and the exception can be handled in the method then handle it there - in this case the return type indicates an exception occured in the method but as it was handled the caller exception handler isnt used.
Module Module1
Sub Main()
Dim i As Integer = -1 '//Default Value
Try
Dim x As New Foo
i = x.method()
MsgBox("This will be reached - because I'm handling the exception in function" & vbCrLf & "Value of i (if i = -1 then still default)" & i.ToString)
Catch ex As Exception
MsgBox("Exception was thrown" & ex.Message)
End Try
End Sub
End Module
Public Class Foo
Function method() As Integer
Try
Throw New Exception("Exception In Foo")
Return -10
Catch ex As Exception
Return 101
End Try
End Function
End Class
Basically you can probably use either of these approaches depending upon you purpose.
Ridge
depends really. If there was an error I would throw the exception up and handle it or perhaps return nothing but would be ideal to throw the exception and catch it and show it to the user for example or handle it your way.
The general rule is to catch the exception and handle it. If a class requires a value back (depends on datatype) then I would return "nothing" (or -1 if integer return type) and check to see what was returned. If it is one of these 2 then it would indicate something went wrong somewhere.
Ruther
it's a bit tricky to explain but bare with me!
say for example a method/function is doing this database job and it returns an object or even a boolean value. Boolean value is easy since you return true in successful operation and false on insuccessful operation.
If we returned an object like a DataTable, and we encountered an exception, we would handle it but if we still can't handle it for whatever reason, then maybe return "Nothing" back to the caller. The caller checks to see what was returned. If not nothing then cool we are in business otherwise if nothing then something went wrong.
Dim theDataTable as DataTable = Me.DoGetData()
if theDataTable is nothing then
'datatable is nothing. Something went wrong!
else
'datatable has data or the object returned is not nothing therefore we are in business
end if
private function DoGetData() as DataTable
try
'do stuff
Dim theDataTable as new DataTable()
'do whatever we want with the datatable such as fill it from the database by OleDbDataAdapter
return theDataTable
catch ex as OleDbException
'handle it or something. If we can't then return nothing:
return Nothing
end try
end function
does this help a bit more I guess this is all down to design and how you want to handle your errors. If its a severe error I would return nothing and check what the error was by some aid of an error log class or something and notify the user that something went wrong and let them do whatever it is they need to do to make it work again perhaps
NewInput