Home Explore Blog CI



neovim

37th chunk of `runtime/doc/vimeval.txt`
9cd32536456054cc5e09382b2e3e4bd9445c4efc2ce882b10000000100000fa1
 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:/

You can catch all errors related to the name "nofunc" by >
	:catch /\<nofunc\>/

You can catch all Vim errors in the ":write" and ":read" commands by >
	:catch /^Vim(\(write\|read\)):E\d\+:/

You can catch all Vim errors by the pattern >
	:catch /^Vim\((\a\+)\)\=:E\d\+:/
<
							*catch-text*
NOTE: You should never catch the error message text itself: >
	:catch /No such variable/
only works in the English locale, but not when the user has selected
a different language by the |:language| command.  It is however helpful to
cite the message text in a comment: >
	:catch /^Vim(\a\+):E108:/   " No such variable


IGNORING ERRORS						*ignore-errors*

You can ignore errors in a specific Vim command by catching them locally: >

	:try
	:  write
	:catch
	:endtry

But you are strongly recommended NOT to use this simple form, since it could
catch more than you want.  With the ":write" command, some autocommands could
be executed and cause errors not related to writing, for instance: >

	:au BufWritePre * unlet novar

There could even be such errors you are not responsible for as a script
writer: a user of your script might have defined such autocommands.  You would
then hide the error from the user.
   It is much better to use >

	:try
	:  write
	:catch /^Vim(write):/
	:endtry

which only catches real write errors.  So catch only what you'd like to ignore
intentionally.

For a single command that does not cause execution of autocommands, you could
even suppress the conversion of errors to exceptions by the ":silent!"
command: >
	:silent! nunmap k
This works also when a try conditional is active.


CATCHING INTERRUPTS					*catch-interrupt*

When there are active try conditionals, an interrupt (CTRL-C) is converted to
the exception "Vim:Interrupt".  You can catch it like every exception.  The
script is not terminated, then.
   Example: >

	:function! TASK1()
	:  sleep 10
	:endfunction

	:function! TASK2()
	:  sleep 20
	:endfunction

	:while 1
	:  let command = input("Type a command: ")
	:  try
	:    if command == ""
	:      continue
	:    elseif command == "END"
	:      break
	:    elseif command == "TASK1"
	:      call TASK1()
	:    elseif command == "TASK2"
	:      call TASK2()
	:    else
	:      echo "\nIllegal command:" command
	:      continue
	:    endif
	:  catch /^Vim:Interrupt$/
	:    echo "\nCommand interrupted"
	:    " Caught the interrupt.  Continue with next prompt.
	:  endtry
	:endwhile

You can interrupt a task here by pressing CTRL-C; the script then asks for
a new command.  If you press CTRL-C at the prompt, the script is terminated.

For testing what happens when CTRL-C would be pressed on a specific line in
your script, use the debug mode and execute the |>quit| or |>interrupt|
command on that line.  See |debug-scripts|.


CATCHING ALL						*catch-all*

The commands >

	:catch /.*/
	:catch //
	:catch

catch everything, error exceptions, interrupt exceptions and exceptions
explicitly thrown by the |:throw| command.  This is useful at the top

Title: Catching Specific Errors, Ignoring Errors, Catching Interrupts, and Catching All Exceptions
Summary
This section provides examples of how to catch specific Vim errors using regular expressions in `:catch` statements, including examples for catching `:unlet` errors, misspelled commands, and errors related to specific function names or commands like `:write` and `:read`. It warns against catching error message text due to localization issues. It also explains how to ignore errors using `try-catch` blocks or the `:silent!` command, emphasizing the importance of catching only the intended errors. Additionally, it covers how to catch interrupts (CTRL-C) as exceptions and provides an example of handling them in a script. Finally, it mentions that `:catch /.*/`, `:catch //`, and `:catch` can catch all types of exceptions, including errors, interrupts, and exceptions thrown by the `:throw` command.