Repository: PowerShell/PowerShell
File: src/System.Management.Automation/engine/LanguagePrimitives.cs (C#)

Code before:
                return System.Convert.ChangeType(value: 0, resultType, CultureInfo.InvariantCulture);
            }

            typeConversion.WriteLine("Converting to integer.");

            try
            {
                if (TryScanNumber(strToConvert, resultType, out object result))
                {
                    return result;
                }

               if (resultType == typeof(BigInteger))
               { 
                    // Adjust NumberStyles to remove AllowHexSpecifier for standard numeric parsing
                    NumberStyles style = NumberStyles.AllowLeadingSign 
                    | NumberStyles.AllowDecimalPoint 
                    | NumberStyles.AllowExponent; 
                    BigInteger parsedValue;
                    
                    if (BigInteger.TryParse(strToConvert, style, NumberFormatInfo.InvariantInfo, out parsedValue))
                    {
                        return parsedValue;
                    }
                    
                    throw new PSInvalidCastException("Failed to convert string to BigInteger.");
                }
                // Fallback conversion for regular numeric types.
                return GetIntegerSystemConverter(resultType).ConvertFrom(strToConvert);
            }
            catch (Exception e)
            {
                // This catch has one extra reason to be generic (Exception e).
                // TypeConverter.ConvertFrom wraps its exceptions in a System.Exception.
                if (e.InnerException != null)
                {
                    e = e.InnerException;
                }

                typeConversion.WriteLine("Exception converting to integer: \"{0}\".", e.Message);
                if (e is FormatException)
                {
                    typeConversion.WriteLine("Converting to integer passing through double.");
                    try
                    {
                        return ConvertNumericThroughDouble(strToConvert, resultType);
                    }
                    catch (Exception ex) // swallow non-severe exceptions
                    {
                        typeConversion.WriteLine("Exception converting to integer through double: \"{0}\".", ex.Message);
                    }

Review comment (suggestion):
The code is in try-catch block where we create generic error message. So we have no need re-throw and can use BigInteger.Parse() instead of BigInteger.TryParse().

Code after:
               if (resultType == typeof(BigInteger))
               { 
                    NumberStyles style = NumberStyles.Integer | NumberStyles.AllowThousands;
                    
                    return BigInteger.Parse(strToConvert, style, NumberFormatInfo.InvariantInfo);
                }
                // Fallback conversion for regular numeric types.
                return GetIntegerSystemConverter(resultType).ConvertFrom(strToConvert);
            }
            catch (Exception e)
            {
                // This catch has one extra reason to be generic (Exception e).
                // TypeConverter.ConvertFrom wraps its exceptions in a System.Exception.
                if (e.InnerException != null)
                {
                    e = e.InnerException;
                }

                typeConversion.WriteLine("Exception converting to integer: \"{0}\".", e.Message);
                if (e is FormatException)
                {
                    typeConversion.WriteLine("Converting to integer passing through double.");
                    try
                    {
                        return ConvertNumericThroughDouble(strToConvert, resultType);
                    }
                    catch (Exception ex) // swallow non-severe exceptions
                    {
                        typeConversion.WriteLine("Exception converting to integer through double: \"{0}\".", ex.Message);
                    }
                }

                throw new PSInvalidCastException("InvalidCastFromStringToInteger", e,
                    ExtendedTypeSystem.InvalidCastExceptionWithInnerException,
                    strToConvert, resultType.ToString(), e.Message);
            }
        }

        private static object ConvertStringToDecimal(object valueToConvert,
                                                     Type resultType,
                                                     bool recursion,
                                                     PSObject originalValueToConvert,
                                                     IFormatProvider formatProvider,
                                                     TypeTable backupTable)
        {
            Diagnostics.Assert(valueToConvert is string, "Value to convert must be a string");
            var strToConvert = valueToConvert as string;

            if (strToConvert.Length == 0)
            {
                typeConversion.WriteLine("Returning numeric zero.");