diff --git a/src/SimpleSAML/XHTML/Template.php b/src/SimpleSAML/XHTML/Template.php
index 149ccb0c4d8fd2ac099dbb0b2c56fe6cf69cba56..5387aea68dcc6eea50e07b1caeacf1e47602892d 100644
--- a/src/SimpleSAML/XHTML/Template.php
+++ b/src/SimpleSAML/XHTML/Template.php
@@ -165,13 +165,19 @@ class Template extends Response
 
         // check if we need to attach a theme controller
         $controller = $this->configuration->getOptionalString('theme.controller', null);
-        if (
-            $controller !== null
-            && class_exists($controller)
-            && in_array(TemplateControllerInterface::class, class_implements($controller))
-        ) {
-            /** @var \SimpleSAML\XHTML\TemplateControllerInterface $this->controller */
-            $this->controller = new $controller();
+        if ($controller !== null) {
+            if (
+                class_exists($controller)
+                && in_array(TemplateControllerInterface::class, class_implements($controller))
+            ) {
+                /** @var \SimpleSAML\XHTML\TemplateControllerInterface $this->controller */
+                $this->controller = new $controller();
+            } else {
+                throw new Error\ConfigurationError(
+                    'Invalid controller was configured in `theme.controller`. ' .
+                    ' Make sure the class exists and implements the TemplateControllerInterface.'
+                );
+            }
         }
 
         $this->fileSystem = new Filesystem();