-1

I'm currently working on a project and I've seen this line in multiple classes.

int.Parse(race); // to trigger any possible errors

Now, I'm guessing they're doing this to throw any possible errors they can, but I don't see this as a good way to do something like this, and I also don't know how to improve on it.

I was wondering if someone coming from a professional background of C# can give me some insight to handling things like this, and how they would handle it?

u526410
  • 13
  • 1
  • 1
    It´s a bad idea to rely on exceptions to handle your work-flow. When you can *avoid* an eception, you should do so. Exceptions are - well - *exceptional*, nothing *regular*. However to suggest a better way we need to know what your input-data actually is and what you consider to be a valid string and what invalid. – MakePeaceGreatAgain Jan 13 '18 at 22:43
  • 1
    I addition to what I´ve said above, this question is mostly opinion-based, or at least to broad, as we don´t know what you expect and what your data is. So we can´t determine what is the correct or even a better way. – MakePeaceGreatAgain Jan 13 '18 at 22:47
  • My data is a string of a string array. – u526410 Jan 13 '18 at 23:01
  • Welcome to SO. Please read [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) and modify question accordingly to minimal working example. Without posting your code you risk removal of your question. With your stated trail and error code... people are more willing to help you so both can learn. Enjoy SO ;-) – ZF007 Jan 13 '18 at 23:17
  • Assuming you want to check if string is integer - following search https://www.bing.com/search?q=c%23+check+string+is+number brings linked duplicate. If you simply want to chitchat about different error handling approaches - SO is not the right place and question should be closed as "opinion based". – Alexei Levenkov Jan 14 '18 at 00:21

1 Answers1

1

If it makes sense to let the exception bubble up when the input string is not a valid integer then you can proceed as is. However, given the exception details are not required elsewhere in the calling code then using TryParse would be the better alternative.

pre C# 7:

int result;
if (int.TryParse(s, out result))
{
    // valid int
}
else
{
    // not valid int
}

as of C# 7:

if (int.TryParse(s, out int result))
{
     // valid int
}
else
{
     // not valid int
}
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • From the question I guess OP considers a number to be *invalid*, as it should "trigger" an error. – MakePeaceGreatAgain Jan 13 '18 at 22:48
  • @HimBromBeere edited to further clarify. – Ousmane D. Jan 13 '18 at 22:52
  • I asked the question more as not how to check it, rather what to do inside the if block, what would be a better way, throwing an exception, writing to the console, or something different. – u526410 Jan 13 '18 at 23:00
  • @u526410 If the method can't handle the exception properly then you let it bubble up to the calling code, and if that can't handle it then let it bubble up until the point where it can be handled. as for whether to write to console, or something different depends on what you're dealing with. – Ousmane D. Jan 13 '18 at 23:07