X
PrevPrev Go to previous topic
NextNext Go to next topic
Last Post 27 Nov 2007 11:22 AM by  anon
Continue on error
 1 Replies
Sort:
You are not authorized to post a reply.
Author Messages

anon



New Member


Posts:
New Member


--
27 Nov 2007 11:22 AM
    hi, I try to understand how I could continue the execution of a procedure while it encounters an error. I found in the IDL help the catch procedure. So I tried the following issued form the IDL help: PRO Test_Error_Stop I = SHIFT(INDGEN(10), -5) PRINT, I F = INTARR(9) ; Define variable A: A = FLTARR(10) ; Establish error handler. When errors occur, the index of the ; error is returned in the variable Error_status: CATCH, Error_status ;This statement begins the error handler: IF Error_status NE 0 THEN BEGIN PRINT, 'Error index: ', Error_status PRINT, 'Error message: ', !ERROR_STATE.MSG ; Handle the error by extending A: A=FLTARR(12) CATCH, /CANCEL ENDIF ; Cause an error: FOR K=0,10 DO BEGIN PRINT, K, I[K] F[I[K]] = K ENDFOR ; Even though an error occurs in the line above, program ; execution continues to this point because the event handler ; extended the definition of A so that the statement can be ; re-executed. HELP, A PRINT, F END The output of this code is the following: IDL> test_error_stop 5 6 7 8 9 0 1 2 3 4 0 5 1 6 2 7 3 8 4 9 Error index: -161 Error message: Attempt to subscript F with is out of range. 0 5 1 6 2 7 3 8 4 9 Attempt to subscript F with is out of range. Execution halted at: TEST_ERROR_STOP 26 E:\Documents and Settings\cottinan.ECLAIRS\Bureau\test_error_stop.pro $MAIN$ So obviously the program continue to run after an error, but why did it restart from the begining ? is there a way to just resume the program from the last step, pass the step that cause an error and go directly to the next one ?

    Deleted User



    New Member


    Posts:
    New Member


    --
    27 Nov 2007 11:22 AM
    I remember also for me, when I was first learning CATCH, that it took a long time for me to understand how and where it was "branching" to. Once I understood it, though, its behavior seemed very intuitive and obvious, so I could not understand why I struggled with it for so many hours when I first learned it. The straightforward behavior rule is: No matter where the error is triggered, once the error passes the execution to the code in the CATCH statement, the execution proceeds forward from that line, executing first the "catch block" error handler and then reexecuting all lines that follow the catch block up to and including the line that triggered the error that is being handled. In short, execution jumps from the error line to the CATCH line and then flows according to normal rules. The only difference from the first pass through CATCH is that 'Error_status' is no longer equal to zero this second time through. If you do not want the lines immediately after the catch block error handler to execute, then the error handler will have to make the line of execution "jump" to a different "entry point" in your code. A GOTO statement will allow that, although many expert programmers say that the GOTO statement is a dangerous and code-share-unfriendly tool. There are other options, of course. For example, the error handler could have a conditional flag that is checked by the next line after the error handler, e.g. if error_handled eq 0 then begin ; Do the routine behavior ; ... endif else begin ; Pick up here after an error is handled ; ... The best approach, however, is to get your error handler as close to the possible error line as possible, I think. If you suspect that a file I/O action, for example, is the most likely to trigger a specific error, insert a CATCH block right above that file I/O codeline. James Jones ITT Technical Support
    You are not authorized to post a reply.