Results 1 to 6 of 6

Thread: Return an error from a function

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2004
    Posts
    263

    Return an error from a function

    I have a function that returns a double. If in the function I decide the value is invalid, is there any way to return "error" instead of a double? Of course I could pass a boolean by reference and set it false and then check for that back in the calling function, but that seems awkward.

    Thoughts?

    Thanks,

    Dave

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Return an error from a function

    How about throw Exception? For example, ArgumentException.

    However, consider the cost of throwing and handling the exception. Is there a value you can return, as a double, that the calling method wouldn't expect?

    For example, will your function always return a positive value? If yes, then you could possibly return a -1 which the calling app could check for.

    Else, you might as well go for the reference boolean. It may seem awkward, but it's just as valid. 3 ways to skin felines.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2004
    Posts
    263

    Re: Return an error from a function

    What is the cost of throwing an exception? That seems like exactly what I want.

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Return an error from a function

    Well, I should explain in context - are you doing this in a class library that could be reused anywhere? It would make sense to throw an ArgumentException there.

    But is this method going to be used a lot? For example, in a loop or get several calls at once? Because if an exception were to be thrown for each call in a loop, it'd slow your application down quite a lot.

    So what I'm trying to say is that if performance is important to you here, then you may want to reconsider throwing an Exception. If you're alright with throwing the Exception because this type of functionality is exactly what you want (as you said) and is also important for your application flow, then by all means...

    I assume you've figured out the syntax for it?

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: Return an error from a function

    There is no cost to exception handling if no exception is ever thrown. However, if an exception IS thrown, catching the exception is slow. One or two, every now and then, won't be noticed. Throw them repeatedly, or throw them in already slow code, and you will begin feeling the lag pretty quickly.

    In general, exceptions should be used for exceptional conditions. An exceptional condition is one that you cannot deal with, and cannot anticipate. Your case doesn't sound exceptional, since you can check the value and find that it is in error, so if it was me, I wouldn't even consider throwing an exception....unless it was in a library where I had no other option. Usually, I want to return a value that is clearly invalid, but if all values for your return are reasonable values, then an exception might be the only alternative.

    By the way, I am currently using code that uses the ByRef solution quite freely (I didn't write it), but the ByRef argument is a string rather than a boolean, and an entire message is returned in that string.
    My usual boring signature: Nothing

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Return an error from a function

    Just to add, I too am currently using similar code in a few of my methods in a web service I'm writing. I don't want to send an entire exception over the wire, so I just have a ref-boolean as an argument.

    Also, to strongly add, I am not Shaggy Hiker's coworker.

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