# HG changeset patch # User Bram Moolenaar # Date 1607887804 -3600 # Node ID 3e7723bab4e57fdde899e75c13d3597f0bb603d6 # Parent 42b507fffe8f3298ba087bf65270a8dd79fe3e8a patch 8.2.2141: a user command with try/catch may not catch an expression error Commit: https://github.com/vim/vim/commit/8143a53c533bc7776c57e5db063d185bdd5750f3 Author: Bram Moolenaar Date: Sun Dec 13 20:26:29 2020 +0100 patch 8.2.2141: a user command with try/catch may not catch an expression error Problem: A user command with try/catch may not catch an expression error. Solution: When an expression fails check for following "|". (closes https://github.com/vim/vim/issues/7469) diff --git a/src/eval.c b/src/eval.c --- a/src/eval.c +++ b/src/eval.c @@ -2158,7 +2158,10 @@ eval0( semsg(_(e_invexpr2), arg); // Some of the expression may not have been consumed. Do not check for - // a next command to avoid more errors. + // a next command to avoid more errors, unless "|" is following, which + // could only be a command separator. + if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|') + eap->nextcmd = check_nextcmd(p); return FAIL; } diff --git a/src/testdir/test_trycatch.vim b/src/testdir/test_trycatch.vim --- a/src/testdir/test_trycatch.vim +++ b/src/testdir/test_trycatch.vim @@ -2199,5 +2199,30 @@ func Test_BufEnter_exception() %bwipe! endfunc +" Test for using try/catch in a user command with a failing expression {{{1 +func Test_user_command_try_catch() + let lines =<< trim END + function s:throw() abort + throw 'error' + endfunction + + command! Execute + \ try + \ | let s:x = s:throw() + \ | catch + \ | let g:caught = 'caught' + \ | endtry + + let g:caught = 'no' + Execute + call assert_equal('caught', g:caught) + END + call writefile(lines, 'XtestTryCatch') + source XtestTryCatch + + call delete('XtestTryCatch') + unlet g:caught +endfunc + " Modeline {{{1 " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker diff --git a/src/testdir/test_vimscript.vim b/src/testdir/test_vimscript.vim --- a/src/testdir/test_vimscript.vim +++ b/src/testdir/test_vimscript.vim @@ -6825,7 +6825,7 @@ func Test_script_lines() \ ]) call assert_report("Shouldn't be able to define function") catch - call assert_exception('Vim(function):E126: Missing :endfunction') + call assert_exception('Vim(function):E1145: Missing heredoc end marker: .') endtry " :change @@ -6845,7 +6845,7 @@ func Test_script_lines() \ ]) call assert_report("Shouldn't be able to define function") catch - call assert_exception('Vim(function):E126: Missing :endfunction') + call assert_exception('Vim(function):E1145: Missing heredoc end marker: .') endtry " :insert @@ -6865,7 +6865,7 @@ func Test_script_lines() \ ]) call assert_report("Shouldn't be able to define function") catch - call assert_exception('Vim(function):E126: Missing :endfunction') + call assert_exception('Vim(function):E1145: Missing heredoc end marker: .') endtry endfunc diff --git a/src/version.c b/src/version.c --- a/src/version.c +++ b/src/version.c @@ -751,6 +751,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 2141, +/**/ 2140, /**/ 2139,