tags:

views:

666

answers:

5

I'm using the code below and occasionally boolUpdate is not TRUE or FALSE and I get an exception, I can't surround this with a TRY CATCH block as it is using 'return', how can I catch this correctly?

if (!Boolean.Parse(boolUpdate)) return true;
+18  A: 

How about using Boolean.TryParse instead?

bool result = false;
Boolean.TryParse( boolUpdate, out result );
return !result;
Ahmad Mageed
When I use TryParse I get the error "No overload for method 'TryParse' takes '1' arguments". I simply tried "if (!Boolean.TryParse(boolUpdate)) return true;", am I missing something? Thanks
Jade M
@Jade look more closely at the example. It takes two arguments!
Rex M
Your missing the out parameter that is set to the value, the return value indicates the boolean was parsed successfully.
csharptest.net
Many thanks all.
Jade M
@tvanfosson: thanks for the edit! I had just added some code but kept yours instead.
Ahmad Mageed
I guess it depends on what input to expect.what if value returned is 1? Wont this be translated to FALSE since TryParse takes a string?Convert.ToBoolean for example, takes alot of different inputs such as integer
Anders Rask
@Anders: yep, it depends on how flexible you want the parsing to be. Convert.ToBoolean takes 1, "false," "true." Also, it considers any integer other than 0 to be true (2, -1, int.MaxValue, int.MinValue). However, it doesn't take numbers in strings, such as "1" or "0," and will throw a FormatException.
Ahmad Mageed
+8  A: 

First, the general case: Just because you return out of a block doesn't mean you can't put it inside of a try/catch:

try
{
    if ( whatever )
        return true;
}

catch ( Exception E )
{
    HandleMyException( E );
    return false;
}

... this is perfectly legal. Meanwhile, as other posters have written, TryParse() is probably what you want in this specific case.

Bob Kaufman
Exception E in catch is unnecessary.
pho3nix
@pho3nix: but handy to have around when debugging.
jcollum
When debugging into the catch, you can hover the catch keyword to see the exception being thrown. But in the example the exception is handled, so it is in fact necessary in that context.
Ronald
I added HandleMyException() in response to @pho3nix's observation... that is, if I'm going to explicitly declare it, I should explicitly *do* something with it.
Bob Kaufman
+5  A: 

The following will return true only when the string is 'true' and not generate exceptions.

  bool value;
  return bool.TryParse(boolUpdate, out value) && value;
csharptest.net
+3  A: 

When boolUpdate does not contain TRUE or FALSE, you should catch the exception offcourse, but what would you like to do when such a situation arises ? You do not wan't to ignore the exception, don't you, since I feel that you want to return from the method anyway ?

Instead of using Boolean.Parse, you can use Boolean.TryParse, wich will return false if the Parse operation failed (the boolUpdate argument in your case doesn't contain true or false, for instance).

Or, you can do this:

try
{
   return Boolean.Parse (boolUpdate)
}
catch(FormatException ex )
{
   return false;
}

But, I would prefer to use TryParse:

bool result;
bool containsBool = Boolean.TryParse (boolUpdate, out result);
return containsBool && result;
Frederik Gheysels
+1 for being the most complete ;)
csharptest.net
A: 

You can try to use Boolean.TryParse...it does not throw, will put the parsed value in the out parameter if the parsing was successful, otherwise it will have the default value, also will return true if the parsing was successful.

 string blah = "true";
 bool val;
 Boolean.TryParse( blah, out val);
 return val;
CSharpAtl