patch 9.0.2059: outstanding exceptions may be skipped

Problem:  outstanding exceptions may be skipped
Solution: When restoring exception state, process remaining outstanding
          exceptions

closes: #13386

Signed-off-by: Christian Brabandt <cb@256bit.org>
Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
diff --git a/src/testdir/test_user_func.vim b/src/testdir/test_user_func.vim
index 8c3f33d..57a093c 100644
--- a/src/testdir/test_user_func.vim
+++ b/src/testdir/test_user_func.vim
@@ -904,7 +904,68 @@
 
   delfunc Defer
   delfunc Foo
+  delfunc Bar
   unlet g:callTrace
 endfunc
 
+" Test for multiple deferred function which throw exceptions.
+" Exceptions thrown by deferred functions should result in error messages but
+" not propagated into the calling functions.
+func Test_multidefer_with_exception()
+  let g:callTrace = []
+  func Except()
+    let g:callTrace += [1]
+    throw 'InnerException'
+    let g:callTrace += [2]
+  endfunc
+
+  func FirstDefer()
+    let g:callTrace += [3]
+    let g:callTrace += [4]
+  endfunc
+
+  func SecondDeferWithExcept()
+    let g:callTrace += [5]
+    call Except()
+    let g:callTrace += [6]
+  endfunc
+
+  func ThirdDefer()
+    let g:callTrace += [7]
+    let g:callTrace += [8]
+  endfunc
+
+  func Foo()
+    let g:callTrace += [9]
+    defer FirstDefer()
+    defer SecondDeferWithExcept()
+    defer ThirdDefer()
+    let g:callTrace += [10]
+  endfunc
+
+  let v:errmsg = ''
+  try
+    let g:callTrace += [11]
+    call Foo()
+    let g:callTrace += [12]
+  catch /TestException/
+    let g:callTrace += [13]
+  catch
+    let g:callTrace += [14]
+  finally
+    let g:callTrace += [15]
+  endtry
+  let g:callTrace += [16]
+
+  call assert_equal('E605: Exception not caught: InnerException', v:errmsg)
+  call assert_equal([11, 9, 10, 7, 8, 5, 1, 3, 4, 12, 15, 16], g:callTrace)
+
+  unlet g:callTrace
+  delfunc Except
+  delfunc FirstDefer
+  delfunc SecondDeferWithExcept
+  delfunc ThirdDefer
+  delfunc Foo
+endfunc
+
 " vim: shiftwidth=2 sts=2 expandtab
diff --git a/src/testdir/test_vim9_class.vim b/src/testdir/test_vim9_class.vim
index 59d5c93..02e99c8 100644
--- a/src/testdir/test_vim9_class.vim
+++ b/src/testdir/test_vim9_class.vim
@@ -8347,10 +8347,10 @@
       public static TruthyFn: func
       static list: list<any> = []
       static four: number = 4
-      static hello: string = 'hello'
+      static str: string = 'hello'
 
-      static def Hello(): string
-        return hello
+      static def Str(): string
+        return str
       enddef
 
       static def Four(): number
@@ -8374,8 +8374,17 @@
         assert_equal(16, 1 << Tests.four)
         assert_equal(8, Tests.four + four)
         assert_equal(8, four + Tests.four)
-        assert_equal('hellohello', Tests.hello .. hello)
-        assert_equal('hellohello', hello .. Tests.hello)
+        assert_equal('hellohello', Tests.str .. str)
+        assert_equal('hellohello', str .. Tests.str)
+
+        # Using class variable for list indexing
+        var l = range(10)
+        assert_equal(4, l[Tests.four])
+        assert_equal([4, 5, 6], l[Tests.four : Tests.four + 2])
+
+        # Using class variable for Dict key
+        var d = {hello: 'abc'}
+        assert_equal('abc', d[Tests.str])
       enddef
     endclass
 
@@ -8390,8 +8399,17 @@
       assert_equal(16, 1 << Tests.four)
       assert_equal(8, Tests.four + Tests.Four())
       assert_equal(8, Tests.Four() + Tests.four)
-      assert_equal('hellohello', Tests.hello .. Tests.Hello())
-      assert_equal('hellohello', Tests.Hello() .. Tests.hello)
+      assert_equal('hellohello', Tests.str .. Tests.Str())
+      assert_equal('hellohello', Tests.Str() .. Tests.str)
+
+      # Using class variable for list indexing
+      var l = range(10)
+      assert_equal(4, l[Tests.four])
+      assert_equal([4, 5, 6], l[Tests.four : Tests.four + 2])
+
+      # Using class variable for Dict key
+      var d = {hello: 'abc'}
+      assert_equal('abc', d[Tests.str])
     enddef
 
     Tests.TruthyFn = Tests.Truthy
@@ -8409,8 +8427,17 @@
     assert_equal(16, 1 << Tests.four)
     assert_equal(8, Tests.four + Tests.Four())
     assert_equal(8, Tests.Four() + Tests.four)
-    assert_equal('hellohello', Tests.hello .. Tests.Hello())
-    assert_equal('hellohello', Tests.Hello() .. Tests.hello)
+    assert_equal('hellohello', Tests.str .. Tests.Str())
+    assert_equal('hellohello', Tests.Str() .. Tests.str)
+
+    # Using class variable for list indexing
+    var l = range(10)
+    assert_equal(4, l[Tests.four])
+    assert_equal([4, 5, 6], l[Tests.four : Tests.four + 2])
+
+    # Using class variable for Dict key
+    var d = {hello: 'abc'}
+    assert_equal('abc', d[Tests.str])
   END
   v9.CheckSourceSuccess(lines)
 enddef
diff --git a/src/testdir/test_vim9_script.vim b/src/testdir/test_vim9_script.vim
index 75a358e..cac8484 100644
--- a/src/testdir/test_vim9_script.vim
+++ b/src/testdir/test_vim9_script.vim
@@ -4725,6 +4725,64 @@
   v9.CheckScriptSuccess(lines)
 enddef
 
+" Test for multiple deferred function which throw exceptions.
+" Exceptions thrown by deferred functions should result in error messages but
+" not propagated into the calling functions.
+def Test_multidefer_with_exception()
+  var lines =<< trim END
+    vim9script
+
+    var callTrace: list<number> = []
+    def Except()
+      callTrace += [1]
+      throw 'InnerException'
+      callTrace += [2]
+    enddef
+
+    def FirstDefer()
+      callTrace += [3]
+      callTrace += [4]
+    enddef
+
+    def SecondDeferWithExcept()
+      callTrace += [5]
+      Except()
+      callTrace += [6]
+    enddef
+
+    def ThirdDefer()
+      callTrace += [7]
+      callTrace += [8]
+    enddef
+
+    def Foo()
+      callTrace += [9]
+      defer FirstDefer()
+      defer SecondDeferWithExcept()
+      defer ThirdDefer()
+      callTrace += [10]
+    enddef
+
+    v:errmsg = ''
+    try
+      callTrace += [11]
+      Foo()
+      callTrace += [12]
+    catch /TestException/
+      callTrace += [13]
+    catch
+      callTrace += [14]
+    finally
+      callTrace += [15]
+    endtry
+    callTrace += [16]
+
+    assert_equal('E605: Exception not caught: InnerException', v:errmsg)
+    assert_equal([11, 9, 10, 7, 8, 5, 1, 3, 4, 12, 15, 16], callTrace)
+  END
+  v9.CheckScriptSuccess(lines)
+enddef
+
 " Keep this last, it messes up highlighting.
 def Test_substitute_cmd()
   new