Results 1 to 2 of 2

Thread: [RESOLVED] Handling exception in classes

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Resolved [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
    steve

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width