Re: Error Handling Advice [message #82703 is a reply to message #82702] |
Mon, 14 January 2013 12:58   |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Matthew Argall writes:
>
>> I guess I will start with some with trial and ::ehem:: error ;-)
>
> So, after a little work, I get a cascade of error messages
>
> ----------------------------
> function add, a, b
> Catch, theError
> IF theError NE 0 THEN BEGIN
> Catch, /CANCEL
> void = Error_Message()
> RETURN, !Null
> ENDIF
>
> RETURN, a + b
> end
>
> pro call_add
> Catch, theError
> IF theError NE 0 THEN BEGIN
> Catch, /CANCEL
> void = Error_Message()
> RETURN
> ENDIF
>
> sum = add(a, b)
>
> IF sum EQ !Null THEN message, 'Sum not valid'
> END
> --------------------------------
>
> Should there be a STOP in there somewhere?
No, there are a TON of errors in this program. You might just as well
find all of them before you move on. :-)
> Is this were "add" would require an ON_ERROR, 2? I can imagine some cases where you would want to keep both CATCH statements...
I would probably write this particular set of routines like this.
function add, a, b, SUCCESS=success
Catch, theError
IF theError NE 0 THEN BEGIN
Catch, /CANCEL
void = Error_Message()
success = 0
RETURN, !Null
ENDIF
success = 1
result = a + b
RETURN, result
end
pro call_add
Catch, theError
IF theError NE 0 THEN BEGIN
Catch, /CANCEL
void = Error_Message()
RETURN
ENDIF
sum = add(a, b, SUCCESS=success)
IF ~success THEN RETURN
IF sum EQ !Null THEN message, 'Sum not valid'
END
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.")
|
|
|