I guess you are using a method like
public function detailAction(\YourVendor\Ext\Domain\Model\MyModel $model)
In that case, it is not that easy because the exception is already thrown in extbase's core. You can take a look how I solve it for my news extension:
/**
* @param RequestInterface $request
* @param ResponseInterface $response
* @throws \Exception
*/
public function processRequest(RequestInterface $request, ResponseInterface $response)
{
try {
parent::processRequest($request, $response);
} catch (\Exception $exception) {
$this->handleKnownExceptionsElseThrowAgain($exception);
}
}
/**
* @param \Exception $exception
* @throws \Exception
*/
private function handleKnownExceptionsElseThrowAgain(\Exception $exception)
{
$previousException = $exception->getPrevious();
if (
$this->actionMethodName === 'detailAction'
&& $previousException instanceof \TYPO3\CMS\Extbase\Property\Exception
&& isset($this->settings['detail']['errorHandling'])
) {
$this->handleNoNewsFoundError($this->settings['detail']['errorHandling']);
} else {
throw $exception;
}
}
In the method handleNoNewsFoundError you can do then anything you want to, e.g. calling $GLOBALS['TSFE']->pageNotFoundAndExit('No news entry found.');.