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:/