Jump to content
Sign in to follow this  
PartyPooper

Exiting Functions

Recommended Posts

PartyPooper

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
PartyPooper

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
Valuater

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
SumTingWong

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
PartyPooper

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
PartyPooper

:-)

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.