Home Explore Blog CI



neovim

28th chunk of `runtime/doc/vimeval.txt`
dbd7fbaeb9ca4f6bc37ebaaa51f1c39211fdee51fbc7f4530000000100000fa2
 are executed first.  This process applies to
			all nested `:try`s inside the loop.  The outermost
			`:endtry` then jumps to the command after the loop.

:try				*:try* *:endt* *:endtry* *E600* *E601* *E602*
:endt[ry]		Change the error handling for the commands between
			`:try` and `:endtry` including everything being
			executed across `:source` commands, function calls,
			or autocommand invocations.

			When an error or interrupt is detected and there is
			a `:finally` command following, execution continues
			after the `:finally`.  Otherwise, or when the
			`:endtry` is reached thereafter, the next
			(dynamically) surrounding `:try` is checked for
			a corresponding `:finally` etc.  Then the script
			processing is terminated.  Whether a function
			definition has an "abort" argument does not matter.
			Example: >
		try | call Unknown() | finally | echomsg "cleanup" | endtry
		echomsg "not reached"
<
			Moreover, an error or interrupt (dynamically) inside
			`:try` and `:endtry` is converted to an exception.  It
			can be caught as if it were thrown by a `:throw`
			command (see `:catch`).  In this case, the script
			processing is not terminated.

			The value "Vim:Interrupt" is used for an interrupt
			exception.  An error in a Vim command is converted
			to a value of the form "Vim({command}):{errmsg}",
			other errors are converted to a value of the form
			"Vim:{errmsg}".  {command} is the full command name,
			and {errmsg} is the message that is displayed if the
			error exception is not caught, always beginning with
			the error number.
			Examples: >
		try | sleep 100 | catch /^Vim:Interrupt$/ | endtry
		try | edit | catch /^Vim(edit):E\d\+/ | echo "error" | endtry
<
					*:cat* *:catch* *E603* *E604* *E605*
:cat[ch] /{pattern}/	The following commands until the next `:catch`,
			`:finally`, or `:endtry` that belongs to the same
			`:try` as the `:catch` are executed when an exception
			matching {pattern} is being thrown and has not yet
			been caught by a previous `:catch`.  Otherwise, these
			commands are skipped.
			When {pattern} is omitted all errors are caught.
			Examples: >
		:catch /^Vim:Interrupt$/	 " catch interrupts (CTRL-C)
		:catch /^Vim\%((\a\+)\)\=:E/	 " catch all Vim errors
		:catch /^Vim\%((\a\+)\)\=:/	 " catch errors and interrupts
		:catch /^Vim(write):/		 " catch all errors in :write
		:catch /^Vim\%((\a\+)\)\=:E123:/ " catch error E123
		:catch /my-exception/		 " catch user exception
		:catch /.*/			 " catch everything
		:catch				 " same as /.*/
<
			Another character can be used instead of / around the
			{pattern}, so long as it does not have a special
			meaning (e.g., '|' or '"') and doesn't occur inside
			{pattern}.
			Information about the exception is available in
			|v:exception|.  Also see |throw-variables|.
			NOTE: It is not reliable to ":catch" the TEXT of
			an error message because it may vary in different
			locales.

					*:fina* *:finally* *E606* *E607*
:fina[lly]		The following commands until the matching `:endtry`
			are executed whenever the part between the matching
			`:try` and the `:finally` is left:  either by falling
			through to the `:finally` or by a `:continue`,
			`:break`, `:finish`, or `:return`, or by an error or
			interrupt or exception (see `:throw`).

							*:th* *:throw* *E608*
:th[row] {expr1}	The {expr1} is evaluated and thrown as an exception.
			If the `:throw` is used after a `:try` but before the
			first corresponding `:catch`, commands are skipped
			until the first `:catch` matching {expr1} is reached.
			If there is no such `:catch` or if the `:throw` is
			used after a `:catch` but before the `:finally`, the
			commands following the `:finally` (if present) up to
			the matching `:endtry` are executed.  If the `:throw`
			is after the `:finally`, commands up to the `:endtry`
			are skipped.  At the `:endtry`, this process applies
			again for the next dynamically surrounding `:try`
			(which may be found in a calling function or sourcing

Title: Vim Script: Exception Handling with try, catch, finally, and throw
Summary
This section delves into Vim script's exception handling mechanisms, detailing how errors and interrupts within ':try' and ':endtry' blocks are treated as exceptions. It explains the use of ':catch' to handle specific exceptions using patterns, providing examples of catching different types of errors and interrupts. The section also covers ':finally', which ensures that certain commands are always executed, and ':throw', which allows for raising custom exceptions. It clarifies the flow of control when exceptions occur and how Vim searches for appropriate ':catch' blocks, including the interaction with ':finally' and dynamically surrounding ':try' blocks.