Home Explore Blog CI



neovim

36th chunk of `runtime/doc/vimeval.txt`
797301d0a1b7b6a8e7f7eb2a3057c4584cda15a1d86b61030000000100000fae

changes global settings which need to be restored on failure or normal exit of
that function or script part.

							*break-finally*
Cleanup code works also when the try block or a catch clause is left by
a ":continue", ":break", ":return", or ":finish".
   Example: >

	:let first = 1
	:while 1
	:  try
	:    if first
	:      echo "first"
	:      let first = 0
	:      continue
	:    else
	:      throw "second"
	:    endif
	:  catch /.*/
	:    echo v:exception
	:    break
	:  finally
	:    echo "cleanup"
	:  endtry
	:  echo "still in while"
	:endwhile
	:echo "end"

This displays "first", "cleanup", "second", "cleanup", and "end". >

	:function! Foo()
	:  try
	:    return 4711
	:  finally
	:    echo "cleanup\n"
	:  endtry
	:  echo "Foo still active"
	:endfunction
	:
	:echo Foo() "returned by Foo"

This displays "cleanup" and "4711 returned by Foo".  You don't need to add an
extra ":return" in the finally clause.  (Above all, this would override the
return value.)

							*except-from-finally*
Using either of ":continue", ":break", ":return", ":finish", or ":throw" in
a finally clause is possible, but not recommended since it abandons the
cleanup actions for the try conditional.  But, of course, interrupt and error
exceptions might get raised from a finally clause.
   Example where an error in the finally clause stops an interrupt from
working correctly: >

	:try
	:  try
	:    echo "Press CTRL-C for interrupt"
	:    while 1
	:    endwhile
	:  finally
	:    unlet novar
	:  endtry
	:catch /novar/
	:endtry
	:echo "Script still running"
	:sleep 1

If you need to put commands that could fail into a finally clause, you should
think about catching or ignoring the errors in these commands, see
|catch-errors| and |ignore-errors|.


CATCHING ERRORS						*catch-errors*

If you want to catch specific errors, you just have to put the code to be
watched in a try block and add a catch clause for the error message.  The
presence of the try conditional causes all errors to be converted to an
exception.  No message is displayed and |v:errmsg| is not set then.  To find
the right pattern for the ":catch" command, you have to know how the format of
the error exception is.
   Error exceptions have the following format: >

	Vim({cmdname}):{errmsg}
or >
	Vim:{errmsg}

{cmdname} is the name of the command that failed; the second form is used when
the command name is not known.  {errmsg} is the error message usually produced
when the error occurs outside try conditionals.  It always begins with
a capital "E", followed by a two or three-digit error number, a colon, and
a space.

Examples:

The command >
	:unlet novar
normally produces the error message >
	E108: No such variable: "novar"
which is converted inside try conditionals to an exception >
	Vim(unlet):E108: No such variable: "novar"

The command >
	:dwim
normally produces the error message >
	E492: Not an editor command: dwim
which is converted inside try conditionals to an exception >
	Vim:E492: Not an editor command: dwim

You can catch all ":unlet" errors by a >
	:catch /^Vim(unlet):/
or all errors for misspelled command names by a >
	:catch /^Vim:E492:/

Some error messages may be produced by different commands: >
	:function nofunc
and >
	:delfunction nofunc
both produce the error message >
	E128: Function name must start with a capital: nofunc
which is converted inside try conditionals to an exception >
	Vim(function):E128: Function name must start with a capital: nofunc
or >
	Vim(delfunction):E128: Function name must start with a capital: nofunc
respectively.  You can catch the error by its number independently on the
command that caused it if you use the following pattern: >
	:catch /^Vim(\a\+):E128:/

Some commands like >
	:let x = novar
produce multiple error messages, here: >
	E121: Undefined variable: novar
	E15: Invalid expression:  novar
Only the first is used for the exception value, since it is the most specific
one (see |except-several-errors|).  So you can catch it by >
	:catch /^Vim(\a\+):E121:/

Title: Handling Exceptions in Finally Clauses and Catching Errors
Summary
This section warns against using control flow commands like `:continue`, `:break`, `:return`, `:finish`, or `:throw` within a finally clause, as it can disrupt cleanup actions. It highlights that errors or interrupts within a finally clause can cause unexpected behavior. It also explains how to catch specific errors in Vim scripts using try-catch blocks, detailing the format of error exceptions and providing examples for matching error messages based on command names and error codes.