Skip to content

Commit 8143a53

Browse files
committed
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 #7469)
1 parent 2a3cd3a commit 8143a53

4 files changed

Lines changed: 34 additions & 4 deletions

File tree

src/eval.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2158,7 +2158,10 @@ eval0(
21582158
semsg(_(e_invexpr2), arg);
21592159

21602160
// Some of the expression may not have been consumed. Do not check for
2161-
// a next command to avoid more errors.
2161+
// a next command to avoid more errors, unless "|" is following, which
2162+
// could only be a command separator.
2163+
if (eap != NULL && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
2164+
eap->nextcmd = check_nextcmd(p);
21622165
return FAIL;
21632166
}
21642167

src/testdir/test_trycatch.vim

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2199,5 +2199,30 @@ func Test_BufEnter_exception()
21992199
%bwipe!
22002200
endfunc
22012201

2202+
" Test for using try/catch in a user command with a failing expression {{{1
2203+
func Test_user_command_try_catch()
2204+
let lines =<< trim END
2205+
function s:throw() abort
2206+
throw 'error'
2207+
endfunction
2208+
2209+
command! Execute
2210+
\ try
2211+
\ | let s:x = s:throw()
2212+
\ | catch
2213+
\ | let g:caught = 'caught'
2214+
\ | endtry
2215+
2216+
let g:caught = 'no'
2217+
Execute
2218+
call assert_equal('caught', g:caught)
2219+
END
2220+
call writefile(lines, 'XtestTryCatch')
2221+
source XtestTryCatch
2222+
2223+
call delete('XtestTryCatch')
2224+
unlet g:caught
2225+
endfunc
2226+
22022227
" Modeline {{{1
22032228
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker

src/testdir/test_vimscript.vim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6825,7 +6825,7 @@ func Test_script_lines()
68256825
\ ])
68266826
call assert_report("Shouldn't be able to define function")
68276827
catch
6828-
call assert_exception('Vim(function):E126: Missing :endfunction')
6828+
call assert_exception('Vim(function):E1145: Missing heredoc end marker: .')
68296829
endtry
68306830

68316831
" :change
@@ -6845,7 +6845,7 @@ func Test_script_lines()
68456845
\ ])
68466846
call assert_report("Shouldn't be able to define function")
68476847
catch
6848-
call assert_exception('Vim(function):E126: Missing :endfunction')
6848+
call assert_exception('Vim(function):E1145: Missing heredoc end marker: .')
68496849
endtry
68506850

68516851
" :insert
@@ -6865,7 +6865,7 @@ func Test_script_lines()
68656865
\ ])
68666866
call assert_report("Shouldn't be able to define function")
68676867
catch
6868-
call assert_exception('Vim(function):E126: Missing :endfunction')
6868+
call assert_exception('Vim(function):E1145: Missing heredoc end marker: .')
68696869
endtry
68706870
endfunc
68716871

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,8 @@ static char *(features[]) =
750750

751751
static int included_patches[] =
752752
{ /* Add new patch number below this line */
753+
/**/
754+
2141,
753755
/**/
754756
2140,
755757
/**/

0 commit comments

Comments
 (0)