How to return exit code from Application
BlitzMax Forums/BlitzMax Beginners Area/How to return exit code from Application
| ||
Hello! Please excuse my probably *very* dumb question, but I'm new to (Blitz)Basic. How do I return an (integer) value from a BlitzMax application to the calling environment? I know, that there is the "end" statement, but it does not seem to expect any integer argument... Thanks in advance for any answer! |
| ||
This is just a guess, ok. But cannot you use Return to leave the program? Ok so this wont work if you return from within a function. But if its in the main program doesnt it return that value? Print 15 Return 20 Print 22Id have a look to see if it does return the value. But I have no idea how to ;( |
| ||
Hmmm, it seems to work when placed in the main program - but, unfortunately, this approach does not fully meets my needs as it fails within a function... Is there a generic solution? |
| ||
ok, use "OnEnd", and make sure the value you want is global? No thats a stpid idea cos onend is a function Just "Goto" a label, and return the global value you want from there. But 1)maybe the program still thinks you are in the function. 2) everyone will shout at you for useing goto |
| ||
The generic solution is to have your function return control all the way back to your 'main' program - which returns the value. |
| ||
You could also use an exception to perform an immediate exit. Something like this (untested) :-Type MyFatalException Field errorCode EndType Try ' main program stuff goes here ' if there's an error throw an exception with the return code in it like so :- ' If someErrorOccurred except:MyFatalException = New MyFatalException except.errorCode = 2 Throw except EndIf Catch e:MyFatalException Return e.errorCode End Try ' We got to the end safely Return 0 |
| ||
Good morning! Thanks for all your responses! I could not believe, that BlitzMax does not have a function like "System.exit(value)", but your answers indicate that I'll have to achieve this functionality myself... Strange: BlitzMax is the first programming language I've seen that lacks such a function (ok, I still can't believe ;-) ) I'll try the exception-based approach. |
| ||
End should possibly accept a return value... |
| ||
That's what I tried first... It does not - the compiler complains the given "integer expression" |
| ||
You could store your exit value into a global variable and then have OnEnd( ) call a custom function that simply returns that global variable. Not sure if that'd actually return the value to the OS though. |
| ||
Well, this approach has already been suggested before - but it does not work as it does not pass the exit code to the operating system. I'm now using the exception-based approach. |
| ||
I could not believe, that BlitzMax does not have a function like "System.exit(value)" because you should have your function return control all the way back to your 'main' program - which returns the value. |
| ||
Well, I have a different opinion - but that's not worth starting a discussion about programming principles. I'm now using the exception-based approach, and that looks ok. Thanks for all your help! |