There are several wordings for this depending on what programming languages you are used to, but none of them turned up results for me. Also, neither help abort() nor help raise() nor help throw() gave me anything.

How would you explicitly throw an error? How would you explicitly raise an error? How would you explicitly abort?

I have in the past just written my errormessage directly, which is of course invalid code and raises an error, but I'd prefer to be clear about what I'm doing here.

function may_fail() abort
  let l:message = system('something')
  if v:shell_error
    there was a problem with something <- can I use an actual abort() or something here?
  • 1
    :h throw-catch? – muru Mar 13 at 6:41
  • Yes indeed, thanks! I see now that :h throw was what I needed, but normally for vimscript questions I'm supposed to add () to find functions. – TamaMcGlinn Mar 13 at 6:43
  • 1
    throw is an Ex command not a function. :) The magic string for those is the : prefix rather than the () suffix: :h :throw. – B Layer Mar 13 at 6:46
  • 2
    If you're not sure which one it is two options are 1) do a general search with :helpgrep or 2) look for it in both :h function-list and :h ex-cmd-index. – B Layer Mar 13 at 6:55

Ok, I found the answer a little later on StackOverflow. It's not clear to me whether these vimscript questions should be on vi.stackexchange or on SO.

Use echoerr to output an error and continue, and throw to abort / raise an error:

echoerr 'some command failed with: '
throw l:output
  • 2
    Vimscript is absolutely on-topic here. Anything related to official VIm and NeoVim is. – B Layer Mar 13 at 6:51
  • 2
    Technically, anything vi-like enough :) – D. Ben Knoble Mar 13 at 13:57
  • Well, my answer wasn't intended to be comprehensive but, yes. :) – B Layer Mar 15 at 8:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.