Results 1 to 4 of 4

Thread: Error handling in functions? how should I do it?

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Error handling in functions? how should I do it?

    what's the proper way of doing error handling in a function? This is what I usually do:
    VB Code:
    1. Public Shared Sub ResizeImage(ByVal srcPath As String, ByVal destPath As String...
    2.         Dim srcImage As Image
    3.         Try
    4.             srcImage = Image.FromFile(srcPath)
    5.         Catch ex As Exception
    6.             Throw New ArgumentException("Error opening the source image", "srcPath", ex)
    7.         End Try
    8.         srcImage.Dispose()
    9.     End Sub

    should I just let the code throw an exception by itself? or is it better to make a new exception like that?
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    If you aren't going to try to handle the error or if you plan on just passing the error up then I'd let it do it itself. In other words I wouldn't try to catch the error if I am only going to forward it on. Otherwise lets say you want the path to be only local and tested and it wasn't then I would have it throw its own exception.

  3. #3

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    k, so most the time you dont put error handlers in these kind of functions, right?
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Right, at lest that is how I do it anyway. I usually only put error handling in functions if I will be trying something else in the event of an error or in is some custom error handling for the function. Another example of when I WOULD is if an argument must be within a certain range and isn't or an Enum is an invalid number.

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