Home Explore Blog CI



neovim

41th chunk of `runtime/doc/vimeval.txt`
419ad02e8267f2bc3ffb5d2e5505973458f2e6fd6cacf38b0000000100000fa7
 "abort" flag or in a command
after ":silent!", control flow goes to the following line, and outside
functions, control flow goes to the line following the outermost ":endwhile"
or ":endif".  On the other hand, errors should be catchable as exceptions
(thus, requiring the immediate abortion).

This problem has been solved by converting errors to exceptions and using
immediate abortion (if not suppressed by ":silent!") only when a try
conditional is active.  This is no restriction since an (error) exception can
be caught only from an active try conditional.  If you want an immediate
termination without catching the error, just use a try conditional without
catch clause.  (You can cause cleanup code being executed before termination
by specifying a finally clause.)

When no try conditional is active, the usual abortion and continuation
behavior is used instead of immediate abortion.  This ensures compatibility of
scripts written for Vim 6.1 and earlier.

However, when sourcing an existing script that does not use exception handling
commands (or when calling one of its functions) from inside an active try
conditional of a new script, you might change the control flow of the existing
script on error.  You get the immediate abortion on error and can catch the
error in the new script.  If however the sourced script suppresses error
messages by using the ":silent!" command (checking for errors by testing
|v:errmsg| if appropriate), its execution path is not changed.  The error is
not converted to an exception.  (See |:silent|.)  So the only remaining cause
where this happens is for scripts that don't care about errors and produce
error messages.  You probably won't want to use such code from your new
scripts.

							*except-syntax-err*
Syntax errors in the exception handling commands are never caught by any of
the ":catch" commands of the try conditional they belong to.  Its finally
clauses, however, is executed.
   Example: >

	:try
	:  try
	:    throw 4711
	:  catch /\(/
	:    echo "in catch with syntax error"
	:  catch
	:    echo "inner catch-all"
	:  finally
	:    echo "inner finally"
	:  endtry
	:catch
	:  echo 'outer catch-all caught "' .. v:exception .. '"'
	:  finally
	:    echo "outer finally"
	:endtry

This displays: >
    inner finally
    outer catch-all caught "Vim(catch):E54: Unmatched \("
    outer finally
The original exception is discarded and an error exception is raised, instead.

							*except-single-line*
The ":try", ":catch", ":finally", and ":endtry" commands can be put on
a single line, but then syntax errors may make it difficult to recognize the
"catch" line, thus you better avoid this.
   Example: >
	:try | unlet! foo # | catch | endtry
raises an error exception for the trailing characters after the ":unlet!"
argument, but does not see the ":catch" and ":endtry" commands, so that the
error exception is discarded and the "E488: Trailing characters" message gets
displayed.

							*except-several-errors*
When several errors appear in a single command, the first error message is
usually the most specific one and therefore converted to the error exception.
   Example: >
	echo novar
causes >
	E121: Undefined variable: novar
	E15: Invalid expression: novar
The value of the error exception inside try conditionals is: >
	Vim(echo):E121: Undefined variable: novar
<							*except-syntax-error*
But when a syntax error is detected after a normal error in the same command,
the syntax error is used for the exception being thrown.
   Example: >
	unlet novar #
causes >
	E108: No such variable: "novar"
	E488: Trailing characters
The value of the error exception inside try conditionals is: >
	Vim(unlet):E488: Trailing characters
This is done because the syntax error might change the execution path in a way
not intended by the user.  Example: >
	try
	    try | unlet novar # | catch | echo v:exception | endtry
	catch /.*/
	    echo "outer catch:" v:exception
	endtry
This displays "outer catch: Vim(unlet):E488: Trailing characters",

Title: Exception Handling Peculiarities and Error Handling Scenarios
Summary
This section delves into the nuances of exception handling in Vimscript, particularly when interacting with older scripts or encountering syntax errors. It explains how Vim handles errors within 'try' blocks to maintain compatibility while enabling exception catching. It also covers scenarios where syntax errors or multiple errors within a single command influence the exception that is thrown, emphasizing that syntax errors take precedence in exception generation to prevent unintended execution paths.