[RESOLVED] Handling exception in classes
Hello,
I have the following code in a class of mine. The purpose of this class is to get the balance from a web server. Just in case something goes wrong with getting the balance. I will handle a exception. However, all this is easy to do. But I am left wondering what do I return in my catch statement.
Most of the examples I looked at just write to the console using:
Console.WriteLine(ex.Message);
That is all very well. But in a real application what do most developers do?
My function at the moment returns void. And I am just wondering what else I would return if the webclient is busy?
Many thanks for any advice,
Code:
'Download only when the webclient is not busy.
If (Not wc.IsBusy) Then
' Sleep for 1/2 second to give the server time to update the balance.
System.Threading.Thread.Sleep(500)
Try
' Download the current balance.
wc.DownloadStringAsync(New Uri(strURL))
Catch ex As WebException
Console.Write("GetBalance(): " & ex.Message)
End Try
Else
Console.Write("Busy please try again")
End If
Re: Handling exception in classes
Change the return type from void to bool and return true for success and false for failure. The alternative is to not catch the exception at all and let it bubble up to the application, which may be completely appropriate. You might also catch the exception, wrap in your own custom exception and then throw that. That allows you to provide error information specific to your type and method.