comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » CATCH inside a FOR loop and out?
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
CATCH inside a FOR loop and out? [message #88322] Mon, 14 April 2014 12:25 Go to next message
cgguido is currently offline  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 #88323 is a reply to message #88322] Mon, 14 April 2014 12:30 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Gianguido Cianci writes:

> 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?

You can put as many CATCH error handlers in your code as you like. Only
one is in effect at any one time. (The last one to be executed as you
step through the code.) So, just write another error handler for
NewError.

Cheers,

David


--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
Re: CATCH inside a FOR loop and out? [message #88324 is a reply to message #88322] Mon, 14 April 2014 12:30 Go to previous messageGo to next message
cgguido is currently offline  cgguido
Messages: 195
Registered: August 2005
Senior Member
Also, is there a useful IDL tutorial on error handling with IDL out there?

Thanks,
G
Re: CATCH inside a FOR loop and out? [message #88325 is a reply to message #88324] Mon, 14 April 2014 12:31 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Gianguido Cianci writes:

> Also, is there a useful IDL tutorial on error handling with IDL out there?

Uh, you could pretty much look at any program in the Coyote Library. :-)

Cheers,

David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
Re: CATCH inside a FOR loop and out? [message #88337 is a reply to message #88325] Tue, 15 April 2014 12:31 Go to previous messageGo to next message
chris_torrence@NOSPAM is currently offline  chris_torrence@NOSPAM
Messages: 528
Registered: March 2007
Senior Member
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 #88344 is a reply to message #88337] Tue, 15 April 2014 21:23 Go to previous messageGo to next message
cgguido is currently offline  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 Go to previous messageGo to next message
Craig Markwardt is currently offline  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 #88371 is a reply to message #88359] Thu, 17 April 2014 10:08 Go to previous messageGo to next message
chris_torrence@NOSPAM is currently offline  chris_torrence@NOSPAM
Messages: 528
Registered: March 2007
Senior Member
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
Re: CATCH inside a FOR loop and out? [message #88377 is a reply to message #88371] Thu, 17 April 2014 13:27 Go to previous messageGo to next message
Michael Galloy is currently offline  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 Go to previous messageGo to next message
Craig Markwardt is currently offline  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 Go to previous message
chris_torrence@NOSPAM is currently offline  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
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: DLM "unload"?
Next Topic: traces plot outside of x-axis boundaries

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 13:46:48 PDT 2025

Total time taken to generate the page: 0.00618 seconds