How to return exit code from Application

BlitzMax Forums/BlitzMax Beginners Area/How to return exit code from Application

Rozek(Posted 2006) [#1]
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!


H&K(Posted 2006) [#2]
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 22
Id have a look to see if it does return the value. But I have no idea how to ;(


Rozek(Posted 2006) [#3]
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?


H&K(Posted 2006) [#4]
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


Brendane(Posted 2006) [#5]
The generic solution is to have your function return control all the way back to your 'main' program - which returns the value.


Brendane(Posted 2006) [#6]
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



Rozek(Posted 2006) [#7]
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.


Mark Tiffany(Posted 2006) [#8]
End should possibly accept a return value...


Rozek(Posted 2006) [#9]
That's what I tried first...

It does not - the compiler complains the given "integer expression"


SculptureOfSoul(Posted 2006) [#10]
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.


Rozek(Posted 2006) [#11]
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.


FlameDuck(Posted 2006) [#12]
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.



Rozek(Posted 2006) [#13]
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!