To get a trial key
fill out the form below
Team License (standard version)
Enterprise License (extended version)
* By clicking this button you agree to our Privacy Policy statement

** This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
GBP
RUB
* By clicking this button you agree to our Privacy Policy statement

** This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
Message submitted.

Your message has been sent. We will email you at


If you haven't received our response, please do the following:
check your Spam/Junk folder and click the "Not Spam" button for our message.
This way, you won't miss messages from our team in the future.

>
>
>
V3108. It is not recommended to return …
Analyzer Diagnostics
General Analysis (C++)
General Analysis (C#)
General Analysis (Java)
Diagnosis of micro-optimizations (C++)
Diagnosis of 64-bit errors (Viva64, C++)
MISRA errors
AUTOSAR errors
Additional information
Contents

V3108. It is not recommended to return null or throw exceptions from 'ToString()' method.

Aug. 15, 2016

The analyzer detected that an overridden 'ToString()' method returns 'null' or throws an exception.

Consider the following example:

public override string ToString()
{
  return null;
}

It is very likely that this method will be called to get a string representation of an instance at runtime or during debugging. Since the programmer is not likely to test the function’s return result for 'null', using it may lead to throwing 'NullReferenceException'. If you need to return an empty or unknown value of an instance’s string representation, return an empty string:

public override string ToString()
{
  return string.Empty;
}

Another example of poor implementations of the 'ToString()' method is when it throws exceptions:

public override string ToString()
{
  if(hasError)
    throw new Exception();
  ....
}

It is very likely that this method will be called by the user of the class at a point where exceptions are not expected to be thrown and handled, for example in a destructor.

If you want the method to issue an error message when generating an object’s string representation, return its text as a string or log the error in some way:

public override string ToString()
{
  if(hasError)
  {
    LogError();
    return "Error encountered";
  }
  ....
}

This diagnostic is classified as:

You can look at examples of errors detected by the V3108 diagnostic.

This website uses cookies and other technology to provide you a more personalized experience. By continuing the view of our web-pages you accept the terms of using these files. If you don't want your personal data to be processed, please, leave this site.
Learn More →
Accept