You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SonarLint raises S2372: Remove the exception throwing from this property getter, or refactor the property into a method. MSDN Rule CA1065 allows some instances, in particular ObjectDisposedException.
Property Get Methods
Properties are basically smart fields. Therefore, they should behave like a field as much as possible. Fields do not throw exceptions and neither should properties. If you have a property that throws an exception, consider making it a method.
The following exceptions are allowed to be thrown from a property get method:
System.InvalidOperationException and all derivatives (including System.ObjectDisposedException)
Repro steps
The following code will raise a warning S2372, which is wrong according to CA1065
public override bool CanRead
{
get
{
if (m_Handler == null) throw new ObjectDisposedException("AtomicFileStream");
return m_Handler.FileStream.CanRead;
}
}
Expected behavior
The rule should not trigger in the example code in the repro step given
Actual behavior
The rule is triggered, making a user think they have to change code when it is fine according to MSDN guidelines.
Known workarounds
None
Related information
SonarC# Version 3.4.0.x
Visual Studio Version 2015
The text was updated successfully, but these errors were encountered:
Description
SonarLint raises S2372: Remove the exception throwing from this property getter, or refactor the property into a method. MSDN Rule CA1065 allows some instances, in particular ObjectDisposedException.
Property Get Methods
Properties are basically smart fields. Therefore, they should behave like a field as much as possible. Fields do not throw exceptions and neither should properties. If you have a property that throws an exception, consider making it a method.
The following exceptions are allowed to be thrown from a property get method:
Repro steps
The following code will raise a warning S2372, which is wrong according to CA1065
Expected behavior
The rule should not trigger in the example code in the repro step given
Actual behavior
The rule is triggered, making a user think they have to change code when it is fine according to MSDN guidelines.
Known workarounds
None
Related information
The text was updated successfully, but these errors were encountered: