Skip to content

Commit 7a05b38

Browse files
committed
Try Catch
1 parent 8e121a6 commit 7a05b38

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

Orm/Xtensive.Orm.SqlServer/Sql.Drivers.SqlServer/InternalHelpers.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,18 +136,21 @@ private static UInt128 FromSqlDecimalData(int[] a) =>
136136

137137
internal static decimal TruncateToNetDecimal(SqlDecimal sqlDecimal)
138138
{
139-
var inputData = sqlDecimal.Data;
140-
if (sqlDecimal.Scale is var scale && (scale > 28 || inputData[3] != 0)) {
139+
try {
140+
return sqlDecimal.Value; // throws OverflowException is the value is out of decimal range.
141+
}
142+
catch (OverflowException) {
143+
var inputData = sqlDecimal.Data;
144+
var scale = sqlDecimal.Scale;
141145
var u128 = FromSqlDecimalData(inputData);
142146

143147
for (; scale > 0 && u128 > Max96bitValue; --scale) {
144148
(u128, _) = UInt128.DivRem(u128, Ten);
145149
}
146150

147-
if (u128 <= Max96bitValue) {
148-
return new((int) u128, (int) (u128 >> 32), (int) (u128 >> 64), !sqlDecimal.IsPositive, scale);
149-
}
151+
return u128 <= Max96bitValue
152+
? new((int) u128, (int) (u128 >> 32), (int) (u128 >> 64), !sqlDecimal.IsPositive, scale)
153+
: throw;
150154
}
151-
return sqlDecimal.Value; // throws OverflowException is the value is out of decimal range.
152155
}
153156
}

0 commit comments

Comments
 (0)