amokoura Posted May 6, 2008 Share Posted May 6, 2008 (edited) UPDATE: General template for "exception handling":$exceptionCode = 0 ;;; TRY Do ; Perform possibly erroneous functions ; In case of error: ; $exceptionCode = [integer] ; ExitLoop While True ;;; CATCH Switch $exceptionCode Case 0 ; no exceptions Case [integer]; specific exception case Else ; unknown exception EndSwitch ;;;;FINALLY ; Perform finalizing functions ; If we want to pass exception code back in call stack: ; setError($exceptionCode)oÝ÷ Ù³óOöì7¨ÖÊZËrIf the functions fail inside Try, we'll skip to Switch that checks the exceptions. In the end Finally should be executed in any case. Edited May 6, 2008 by amokoura Link to comment Share on other sites More sharing options...
Generator Posted May 6, 2008 Share Posted May 6, 2008 I had ADODB objects in use so the connections needed to be closed after use. If the function had many exit points, I might've used the Close method in many places. So I needed a "finally" block, as in traditional exception handling. Here's something for a try-structure, please give your opinion: expandcollapse popup$myExp = 0 ; exception store for catching Do ; try block someFunc() if @error then $myExp = 1 ExitLoop ; exit try EndIf otherFunc() if @error Then $myExp = 2 ExitLoop ; exit try EndIf until True ; the catches Switch $myExp case 1 ; Specific expection 1 case 2 ; Specific exception 2 case Else ;OTHER EXCEPTION EndSwitch ; finally block someReleaseFunc() $comObject.CloseoÝ÷ ØíçîËb¢{j)bÈy:òÁíý[$hKraj×!yÉ,¶ÅÇ©¶*'°íç§tX§jYr².ÖÞ{º×v§ÉƬ{ú®¢×¢¶yǧ¶·¬º[r¢ë!¢é]µë-Ê«r^Â+aÊ«rêëz{lr¸©µ©Ý±çê.µÊ&yjÚ¦ºÞ®È¨ò¢êܡש®âuçZnÞjëh×6$myExp = 0 ; exception store for catching While $myExp=0 someFunc() if @error then $myExp = 1 EndIf otherFunc() if @error Then $myExp = 2 EndIf WEnd ; the catches Switch $myExp case 1 ; Specific expection 1 case 2 ; Specific exception 2 case Else ;OTHER EXCEPTION EndSwitch ; finally block someReleaseFunc() $comObject.Close Link to comment Share on other sites More sharing options...
amokoura Posted May 6, 2008 Author Share Posted May 6, 2008 (edited) *clip* While $myExp=0 someFunc() if @error then $myExp = 1 EndIf otherFunc() if @error Then $myExp = 2 EndIf WEnd *clip* Two problems in your solution: - the While is infinite if $myExp remains 0 - if someFunc() causes error, otherFunc() is still run (Exitloop would solve this) Edited May 6, 2008 by amokoura Link to comment Share on other sites More sharing options...
amokoura Posted May 6, 2008 Author Share Posted May 6, 2008 (edited) I'm so excited of this that I made a general template $exceptionCode = 0 ;;; TRY Do ; Perform possibly erroneous functions ; In case of error: ; $exceptionCode = [integer] ; ExitLoop While True ;;; CATCH Switch $exceptionCode Case 0 ; no exceptions Case [integer]; specific exception case Else ; unknown exception EndSwitch ;;;;FINALLY ; Perform finalizing functions ; If we want to pass exception code back in call stack: ; setError($exceptionCode) Edited May 6, 2008 by amokoura Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now