Skip to content

Commit

Permalink
add: try + catch added for both implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
Konnnst committed Oct 19, 2023
1 parent f2b286e commit a068fd0
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions HW02LAZY/LAZY/Lazy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,14 @@ public LazySingleThread(Func<T> supplier)
public T? Get()
{
if (!_isCalculated) {
_result = _supplier();
try
{
_result = _supplier();
}
catch
{
_result = default(T);
}
}
_isCalculated = true;
return _result;
Expand Down Expand Up @@ -76,8 +83,17 @@ public LazyMultiThread(Func<T> supplier)
{
if (Volatile.Read(ref _isCalculated))
return _result;
_result = _supplier();
Volatile.Write(ref _isCalculated, true);

try
{
_result = _supplier();
Volatile.Write(ref _isCalculated, true);
}
catch
{
_result = default(T);
}

return _result;
}
}
Expand Down

0 comments on commit a068fd0

Please sign in to comment.