Our website uses cookies to enhance your browsing experience.
Accept
to the top
close form

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
** By clicking this button you agree to our Privacy Policy statement
close form
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
* By clicking this button you agree to our Privacy Policy statement

close form
Free PVS‑Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

close form
To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

close form
I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

close form
check circle
Message submitted.

Your message has been sent. We will email you at


If you do not see the email in your inbox, please check if it is filtered to one of the following folders:

  • Promotion
  • Updates
  • Spam

Webinar: C++ semantics - 06.11

>
>
>
V6073. It is not recommended to return …
menu mobile close menu
Analyzer diagnostics
General Analysis (C++)
General Analysis (C#)
General Analysis (Java)
Micro-Optimizations (C++)
Diagnosis of 64-bit errors (Viva64, C++)
Customer specific requests (C++)
MISRA errors
AUTOSAR errors
OWASP errors (C#)
Problems related to code analyzer
Additional information
toggle menu Contents

V6073. It is not recommended to return null or throw exceptions from 'toString' / 'clone' methods.

Oct 14 2019

The analyzer has detected an overridden 'toString' or 'clone' method that can return a 'null' value or throw an exception.

The 'toString' / 'clone' method must always return a string / object respectively. Returning an invalid value contradicts the method's implicit contract.

The following example demonstrates incorrect overriding of the 'toString' method:

@Override
public String toString()
{
  return null;
}

The developer who would be using or maintaining the program in the future is likely to call this method to get the textual representation of the object. Since they are unlikely to check the return result for null, using it may lead to throwing a 'NullPointerException'. If you want the method to return an empty or unknown value as the object's textual representation, it is recommended to use an empty string:

@Override
public String toString()
{
  return "";
}

Throwing an exception is another bad practice when implementing the 'toString' method. This is demonstrated by the following synthetic example:

@Override
public String toString()
{
  if(hasError)
  {
    throw new IllegalStateException("toString() method error encountered");
  }
  ....
}

The user of the class is very likely to call this method at a point where no exception throwing and handling is provided for.

If you want an error message to appear when generating an object's textual representation, either return the message text as a string or log the error:

....
@Override
public String toString()
{
  if(hasError)
  {
    logger.warn("toString() method error encountered");
    return "Error encountered";
  }
  ....
}

All said above holds true for the 'clone' method. When calling it, you count only on one of the two possible outcomes:

  • if the copy operation is not supported for this instance, you expect a 'CloneNotSupportedExcexption';
  • if copying is possible, the method is guaranteed to create and return a correct copy of the object.

But you never expect either of the following options:

  • throwing an unexpected exception;
  • getting null instead of the correct copy.

This diagnostic is classified as:

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