CATCH inside a FOR loop and out? [message #88322] |
Mon, 14 April 2014 12:25  |
cgguido
Messages: 195 Registered: August 2005
|
Senior Member |
|
|
Say I want to catch two kinds of erros:
1. if error happens inside a for loop I want to deal with it and then continue with i++
2. if error happens outside the loop I want to return -1
Can this be done?
Thanks,
Gianguido
Here is what I got, which catches erros inside the loop and behaves like I want it to, but I would like to deal with ;NEWERROR differently...
;;--------------------
PRO proerrortest
compile_opt idl2
a = 10*(indgen(10)+1)
FOR i = 0, 9 DO BEGIN
CATCH, Error_status
IF Error_status NE 0 THEN BEGIN
CATCH, /CANCEL
PRINT, 'GCError index: ', Error_status
PRINT, 'GCError message: ', !ERROR_STATE.MSG
i++
if i gt 9 then return
ENDIF
IF i EQ 3 THEN print, i, a[100]
IF i EQ 7 THEN print, i, a[100]
IF i NE 3 AND i NE 7 THEN print, i, a[i]
ENDFOR
;NEWERROR
RETURN
END
|
|
|
|
|
|
|
Re: CATCH inside a FOR loop and out? [message #88344 is a reply to message #88337] |
Tue, 15 April 2014 21:23   |
cgguido
Messages: 195 Registered: August 2005
|
Senior Member |
|
|
Thanks guys!
Chris, I especially like your suggestion!
G
On Tuesday, April 15, 2014 2:31:19 PM UTC-5, Chris Torrence wrote:
> One important point - "catch" is expensive. It's better to put it outside of the for loop, and just use a "start" index to continue. Something like:
>
>
>
> start = 0
>
> catch, err
>
> if (err ne 0) then begin
>
> start = i + 1
>
> print, 'bad error'
>
> endif
>
> for i=start, finish do begin
>
> ...
>
> endfor
>
>
>
> Cheers,
>
> Chris
|
|
|
Re: CATCH inside a FOR loop and out? [message #88359 is a reply to message #88337] |
Wed, 16 April 2014 12:50   |
Craig Markwardt
Messages: 1869 Registered: November 1996
|
Senior Member |
|
|
On Tuesday, April 15, 2014 3:31:19 PM UTC-4, Chris Torrence wrote:
> One important point - "catch" is expensive. It's better to put it outside of the for loop, and just use a "start" index to continue. Something like:
Hey Chris, is there any chance of making IDL exception handling more modern? The C language 'setjmp' exception handling model is not the best. The catch variable is kind of a clunky nuisance, and having to remember to /CANCEL your catch handler is also a bit of a chore.
A revised grammar could be as simple as this,
CATCH BEGIN
;; ... possibly failing code ...
END ELSE BEGIN
;; ... exception handler here ...
ENDELSE
No extra keywords required to be added to the language, no nuisance variables, and cleaner flow. (and no backward compatibility problems) The exception handler can look at any of the crumpteen million !ERR* variables to decide what went wrong.
Craig
|
|
|
|
Re: CATCH inside a FOR loop and out? [message #88377 is a reply to message #88371] |
Thu, 17 April 2014 13:27   |
Michael Galloy
Messages: 1114 Registered: April 2006
|
Senior Member |
|
|
On 4/17/14, 11:08 AM, Chris Torrence wrote:
> On Wednesday, April 16, 2014 1:50:44 PM UTC-6, Craig Markwardt
> wrote:
>>
>> No extra keywords required to be added to the language, no nuisance
>> variables, and cleaner flow. (and no backward compatibility
>> problems) The exception handler can look at any of the crumpteen
>> million !ERR* variables to decide what went wrong.
>>
>
> Crumpteen million! I like that. We should add that as an optional
> tick label for plots.
>
> We've thought about adding a "try/catch/finally" form of error
> handling, which would be similar to what you are proposing. But I
> like your idea of somehow re-using the existing reserved words. We've
> always pushed this feature to the bottom of the list because it was
> unclear how many people would make use of it. But it certainly fits
> in with "modernizing IDL", which has been our goal since IDL 8.0.
>
> Hmmm... Maybe now is the time.
>
> -Chris ExelisVIS
>
CATCH is a routine, not a keyword currently. I, personally, don't think
it would be so bad to add a keyword or two for nice exception handling.
Also, if you add something, I think it should have an optional "finally"
clause as well.
Mike
--
Michael Galloy
www.michaelgalloy.com
Modern IDL: A Guide to IDL Programming (http://modernidl.idldev.com)
Research Mathematician
Tech-X Corporation
|
|
|
Re: CATCH inside a FOR loop and out? [message #88378 is a reply to message #88377] |
Thu, 17 April 2014 13:56   |
Craig Markwardt
Messages: 1869 Registered: November 1996
|
Senior Member |
|
|
On Thursday, April 17, 2014 4:27:53 PM UTC-4, Mike Galloy wrote:
> CATCH is a routine, not a keyword currently. I, personally, don't think
> it would be so bad to add a keyword or two for nice exception handling.
True, but it's a system routine that everybody knows to avoid for their own routines already.
> Also, if you add something, I think it should have an optional "finally"
> clause as well.
I never understood the purpose of FINALLY, when you can just have cleanup code outside the exception handling clauses. Is it more than a visual cue?
Craig
|
|
|
Re: CATCH inside a FOR loop and out? [message #88379 is a reply to message #88378] |
Thu, 17 April 2014 15:06  |
chris_torrence@NOSPAM
Messages: 528 Registered: March 2007
|
Senior Member |
|
|
On Thursday, April 17, 2014 2:56:31 PM UTC-6, Craig Markwardt wrote:
> On Thursday, April 17, 2014 4:27:53 PM UTC-4, Mike Galloy wrote:
>
>> CATCH is a routine, not a keyword currently. I, personally, don't think
>
>> it would be so bad to add a keyword or two for nice exception handling.
>
>
>
> True, but it's a system routine that everybody knows to avoid for their own routines already.
>
>
>
>> Also, if you add something, I think it should have an optional "finally"
>
>> clause as well.
>
>
>
> I never understood the purpose of FINALLY, when you can just have cleanup code outside the exception handling clauses. Is it more than a visual cue?
>
>
>
> Craig
At least in Java, the Finally code is guaranteed to run, even if your "try" or "catch" block does a return from the routine.
-C
|
|
|