Jump to content

Simulate try-catch-finally


Recommended Posts

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Ër

If 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 by amokoura
Link to comment
Share on other sites

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:

$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:òÁíý[$hK­raj×!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

*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 by amokoura
Link to comment
Share on other sites

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 by amokoura
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...