Reputation: 708
I would like to make sure my classes are robust - even if they are simple and seem dumb to an experienced programmer.
Let's say I have a method as written below that accepts a string[] and returns it as an int[].
public static int[] GetIntArrayFromStringArray(string[] stringArray)
{
int i = 0;
int[] ints = new int[stringArray.Length];
foreach (var str in stringArray)
{
ints[i++] = (str != "" ? int.Parse(str) : 0);
}
return ints;
}
Is the best option to use try-catch and throw an exception? When I try to use try-catch, it seems to have issues with my variables not being in scope but I need them to be in a try-catch to catch any errors with the stringArray being null!
Perhaps I should use this?
if (stringArray == null) //do something ...
but not sure what to do in the event of an error ... do I return a null int[] or throw exception?
(I also have to check that the int.Parse(str) doesn't fail - I'm getting to that one but hoped it could be in the try-catch block!)
As I said, these are simple tasks that I want to try and get correct now before I develop too many bad habits. Thanks.
Upvotes: 0
Views: 144
Reputation: 13150
Many methods in the .NET Framework that throw exceptions on invalid input have non-throwing counterparts, usually prefixed with Try
, that return a boolean value instead. For instance, in the case of int.Parse
, you'll have an easier time calling int.TryParse
, e.g.:
public static int[] GetIntArrayFromStringArray(string[] stringArray)
{
int i = 0;
int[] ints = new int[stringArray.Length];
foreach (var str in stringArray)
{
int a = 0
int.TryParse(str, out a)
ints[i++] = a;
}
return ints;
}
Upvotes: 0
Reputation: 328
Here is how you can use try - catch:
public static int[] GetIntArrayFromStringArray(string[] stringArray)
{
int i = 0;
int[] ints = null;
try {
ints = new int[stringArray.Length];
foreach (var str in stringArray)
{
ints[i++] = (str != "" ? int.Parse(str) : 0);
}
}
catch {
// Throw custom exception
}
return ints;
}
Upvotes: 0
Reputation: 4327
Here an example
foreach (string str in stringArray)
{
int nr = 0;
int.TryParse(str, out nr);
if (nr > 0)
ints[i++] = nr;
}
here is the full code
public static int[] GetIntArrayFromStringArray(string[] stringArray)
{
if (stringArray == null || stringArray.Length == 0)
throw new ArgumentNullException("string array is null or empty");
int i = 0;
int[] ints = new int[stringArray.Length];
foreach (var str in stringArray)
{
int nr = 0;
int.TryParse(str, out nr);
if (nr > 0)
ints[i] = nr;
i++;
}
return ints;
}
Upvotes: 0
Reputation: 13460
public static int[] GetIntArrayFromStringArray(string[] stringArray)
{
if (stringArray == null)
{
throw new ArgumentNullException("stringArray");
}
int count = stringArray.Length;
int[] ints = new int[count];
for (int i = 0; i < count; i++)
{
int intValue;
ints[i] = int.TryParse(stringArray[i], out intValue) ? intValue : 0;
}
return ints;
}
Upvotes: 0
Reputation: 2379
public static int[] GetIntArrayFromStringArray(string[] stringArray)
{
int i = 0;
int[] ints;
int num;
if (stringArray == null && stringArray.Length > 0)
{
ints = new int[stringArray.Length]
foreach (var str in stringArray)
{
if (string.IsNullOrEmpty(str) && int.TryParse(str, num))
{
ints[i++] = num;
}
}
}
return ints;
}
Upvotes: 0
Reputation: 9394
I would throw a custom-exception in the catch-block. So the user of your class can catch for this exception.
Upvotes: 0
Reputation: 102793
Try something simpler for the conversion:
return stringArray.Select(s => IntParseOrDefault(s, 0)).ToArray();
Where IntParseOrDefault is just like this:
int IntParseOrDefault(string s, int defaultVal)
{
int i;
if (!int.TryParse(s, out i)) i = defaultVal;
return i;
}
If you want the method to fail if any of the strings is not a valid integer, then instead of using a default value, throw an InvalidArgumentException when TryParse fails.
Upvotes: 1
Reputation: 359
You can use int.TryParse() instead.
http://msdn.microsoft.com/en-us/library/f02979c7.aspx
Upvotes: 1