website/errors/throws.void.md
<?php declare(strict_types = 1);
class MyException extends \Exception
{
}
/**
* @throws void
*/
function doFoo(): void
{
throw new MyException();
}
The function or method has a @throws void PHPDoc tag, which declares that it does not throw any exceptions. However, PHPStan detected an explicit throw statement (or a call to a function that is known to throw) inside the function body. This is a contradiction -- the code promises not to throw but actually does.
If the function can throw exceptions, update the @throws tag to declare the thrown type:
/**
- * @throws void
+ * @throws MyException
*/
function doFoo(): void
{
throw new MyException();
}
If the function should not throw, remove the throw statement:
/**
* @throws void
*/
function doFoo(): void
{
- throw new MyException();
+ // handle the error differently
}
If the @throws void tag was added by mistake, remove it:
-/**
- * @throws void
- */
function doFoo(): void
{
throw new MyException();
}