Sign in to follow this  
Followers 0
PartyPooper

Exiting Functions

7 posts in this topic

Here is my dilemma:

How do I exit a FileOpenError() back to the main code without running all the lines of code left in Two() after an error?

;....
  Two()
;....

Func Two()
;....
  $OpenFile = FileOpen(@ScriptDir & '\' & $File, 0)
  If $OpenFile = -1 Then FileOpenError()
;....
;....
EndFunc

Func FileOpenError()
  MsgBox(262160, "ERROR", "Unable to open file.")
 ;Return - will only take me back to Two() which I don't want
 ;Exit - will dump me out of the script which I don't want
EndFunc

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Should of mentioned I originally had the following which worked but I wanted to clean up the code:

Func Two()
;....
  $OpenFile = FileOpen(@ScriptDir & '\' & $File, 0)
  If $OpenFile = -1 Then
    MsgBox(262160, "ERROR", "Unable to open file.")
    Return
  EndIf
;....
;....
EndFunc

Edit: those code tags again

Edited by PartyPooper

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Simple

;....
  Two()
;....

Func Two()
;....
  $OpenFile = FileOpen(@ScriptDir & '\' & $File, 0)
  If $OpenFile = -1 Then FileOpenError()
  Return *************** here **********************
;....
;....
EndFunc

Func FileOpenError()
  MsgBox(262160, "ERROR", "Unable to open file.")
;Return - will only take me back to Two() which I don't want
;Exit - will dump me out of the script which I don't want
Return '*******************here *********
EndFunc

8)

Edited by Valuater

NEWHeader1.png

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Or

;....
  Two()
;....

Func Two()
;....
  $OpenFile = FileOpen(@ScriptDir & '\' & $File, 0)
  If $OpenFile = -1 Then Return FileOpenError()
;....
;....
EndFunc

Func FileOpenError()
  MsgBox(262160, "ERROR", "Unable to open file.")
  Return $YourReturnCode
;Exit - will dump me out of the script which I don't want
EndFunc
Edited by SumTingWong

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Sorry Valuater, I should have articulated a little better and expanded my example. I meant:

Func Two()
;some code in here
  $OpenFile = FileOpen(@ScriptDir & '\' & $File, 0)
  If $OpenFile = -1 Then FileOpenError()
;some more code here
;even more code
EndFunc

Using your method would simply return me to the main code whether or not an error occured. Thanks for the response though.

Thanks SumTingWong, I believe I can build on your example. I had a feeling it may have had something to do with returned function values (something I'm yet to understand and master).

Edit: typo's

Edited by PartyPooper

Share this post


Link to post
Share on other sites

:-)

If I was only going to use it once in a function, then I'd agree w0uter but unfortunately I'm opening several files within the same function and several more in other functions so I wanted a way of not repeating myself over and over and have errors creep in, as well as, being able to reduce the size of the script a little.

Share this post


Link to post
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
Sign in to follow this  
Followers 0