diff --git a/tests/test/texception12.pp b/tests/test/texception12.pp
new file mode 100644
index 0000000000..e3fa2aecb6
--- /dev/null
+++ b/tests/test/texception12.pp
@@ -0,0 +1,43 @@
+{ %opt=-O3 }
+program Project1;
+{$mode objfpc}{$H+}
+
+uses SysUtils;
+
+var
+  bar: boolean;
+
+procedure Foo;
+begin
+  while true do begin
+    try
+      try
+        try
+          if bar then
+            raise Exception.Create('');
+          Randomize;
+        finally
+          Randomize;
+          try
+            Randomize;
+          finally
+            Randomize;
+          end;
+        end;
+        Randomize;
+      finally
+        Randomize;
+      end;
+      Randomize;
+    except
+      bar := false;
+      Randomize;
+      halt(0);
+    end;
+  end;
+end;
+
+begin
+  bar := true;
+  Foo;
+end.