xsharp.eu • set exception handler inside the runtime - Page 2
Page 2 of 2

set exception handler inside the runtime

Posted: Wed Oct 20, 2021 2:18 pm
by Chris
Hi Wolfgang,

One way would be to define an Event in the library, with information about the exception and intercept it from your core app. Something like:

Code: Select all

// in a VO dialect library:
DELEGATE ErrorHappenedDelegate(oError AS Exception) AS VOID

STATIC CLASS ErrorHandler
	STATIC EVENT ErrorHappened AS ErrorHappenedDelegate
	STATIC METHOD RaiseError(oError AS Exception) AS VOID
	IF ErrorHandler.ErrorHappened != NULL
		ErrorHandler.ErrorHappened(oError)
	END IF
	STATIC METHOD LinkErrorBlock() AS VOID
		ErrorBlock({|oError| ErrorHandler.RaiseError(oError)})
END CLASS
Then just call ErrorHandler.LinkErrorBlock() to set the new error block and subscribe to the event in your core app, like with

Code: Select all

// in the main core app:
ErrorHandler.ErrorHappened += MyErrorHandler

PROCEDURE MyErrorHandler(oError AS Exception)
? oError:ToString()
.

set exception handler inside the runtime

Posted: Wed Oct 20, 2021 2:23 pm
by wriedmann
Hi Chris,
this is great, thank you very much!
Wolfgang

set exception handler inside the runtime

Posted: Wed Oct 20, 2021 2:57 pm
by robert
Wolfgang,
wriedmann wrote:Hi Chris,
maybe I'm stupid and don't see the wood because of the trees: but how I can do that?
My application is in Core dialect and does not knows about codeblocks, but the DLL knows about it (being in VO dialect).
But in case of an error e method of the main (Core dialect) application should be called.
How can I convert a function pointer passed from the application to a codeblock?
With the solution from Chris you do not need the codeblocks, but fyi the Core dialect does have the ICodeBlock interface. Each code block in X# (both compiler time code blocks as well as macro compiled codeblocks) knows this interface.
The interface is declared as

Code: Select all

INTERFACE ICodeblock
        METHOD	EvalBlock( args PARAMS OBJECT[]) AS OBJECT
        METHOD	PCount AS LONG
END INTERFACE
As you can see there is no Eval() method but an EvalBlock() method and this method takes an array of OBJECT values and
not an array of USUAL values.

Robert

set exception handler inside the runtime

Posted: Wed Oct 20, 2021 3:09 pm
by wriedmann
Hi Robert,
thank you very much!
I will give it a try.
Wolfgang