diff --git a/lib/SimpleSAML/Session.php b/lib/SimpleSAML/Session.php
index 7f482a651e943b6d1d7d9499b625c9e850807570..241b705787a33d9c559092a54dfa69efc01628e0 100644
--- a/lib/SimpleSAML/Session.php
+++ b/lib/SimpleSAML/Session.php
@@ -324,8 +324,11 @@ class SimpleSAML_Session
      *
      * Create a session that should not be saved at the end of the request.
      * Subsequent calls to getInstance() will return this transient session.
+     *
+     * @param Exception|null $exception An exception that made us use a transient session. Specify if you want to log a
+     * message and that exception being thrown after loading the transient session.
      */
-    public static function useTransientSession()
+    public static function useTransientSession($exception = null)
     {
         if (isset(self::$instance)) {
             // we already have a session, don't bother with a transient session
@@ -333,6 +336,16 @@ class SimpleSAML_Session
         }
 
         self::load(new SimpleSAML_Session(true));
+
+        if ($exception instanceof Exception) {
+            if ($exception instanceof SimpleSAML_Error_Exception) {
+                $cause = $exception->getCause();
+                if ($cause instanceof Exception) {
+                    throw $cause;
+                }
+            }
+            throw $exception;
+        }
     }
 
     /**