Wednesday, March 30, 2011

Re: If/Else for a Catch statement?

On 31/03/11 08:13, howard Schwartz wrote:
> Hi,
>
> I would like to do something like this, to process exceptions:
>
> try
> if catch /pattern/
> do this and that
> else
> do the ordinary thing
> endif
> endtry
>
> Of course this is not real code, but catches do act like an ``if'', but
> I do not know of a clean way to do an `else'. My sloppy solution was to
> define a
> variable to do it like this:
>
> try
> let s:number = 0
> catch /pattern/
> do this and that
> let s:number = 1
> finally
> if number < 1
> do the ordinary things
> endif
> endtry
>
> Any better way to do an if/else conditional, with a catch statement?

Well, nothing can be caught if there is nothing between the try and the
catch.

Otherwise you seem to have answered yourself:

try
let s:exception = 0
" do something that might trigger an exception
catch /pat1/
let s:exception = 1
" do this1-and-that1
catch /pat2/
let s:exception = 2
" do this2-and-that2
catch " any other exception
let s:exception = 999
" do this999-and-that999
finally
if !s:exception
" there was no exception
" do this0-and-that0
endif
" do any final cleanup which might be needed
" whether or not an exception was triggered
unlet s:exception
endtry


Best regards,
Tony.
--
The only really decent thing to do behind a person's back is pat it.

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments: