xsharp.eu • Exception handling
Page 1 of 1

Exception handling

Posted: Tue Sep 10, 2019 9:53 am
by wriedmann
Hello,

I'm now trying to find a correct error handling procedure with central exception handlers, but I'm not sure if this is a good practice.

Please see the following code:

Code: Select all

function Start( ) as void
	local oProcessing as Processing

	oProcessing := Processing{}
	oProcessing:ExceptionHandler := HandleException
	oProcessing:Process()

return

function HandleException( oEx as Exception ) as logic

	System.Console.WriteLine( "Exception occurred:" + oEx:Message )

	return true

delegate ExceptionHandler( oEx as Exception ) as logic

class Processing

constructor()

	return

public property ExceptionHandler as ExceptionHandler auto

method Process() as void
	local u as object

	System.Console.WriteLine("Testing Exception Handler")

	try

	u := "Hi"
	u := int( u ) + 123

	catch oEx as Exception

	if self:ExceptionHandler != null
		self:ExceptionHandler:Invoke( oEx )
	endif

	end try

	return

end class
This code works as expected, but I have my doubts if this is the optimal solution.

As background: I'm writing a production planning software that can be used with a GUI and only from command line (as planned task), and delegating the error handling from the library to the calling executable helps me cover all needs.

Wolfgang

P.S. this is inspired by the VO Errorblock and the discussion about error handling in migrated VO applications

Exception handling

Posted: Tue Sep 10, 2019 2:25 pm
by Chris
Hi Wolfgang,

IMO there's no "good" or "bad" practice, unless you are causing some problem. I see no problem with the code you posted, so if it covers a need for you, then I think it's fine to use it. I only did not understand why you needed to use the delegate, so you plan to change which exception handling function will be called during the execution of your app? Can you explain why?

Exception handling

Posted: Tue Sep 10, 2019 2:31 pm
by wriedmann
Hi Chris,
I only did not understand why you needed to use the delegate, so you plan to change which exception handling function will be called during the execution of your app? Can you explain why?
exactly. The GUI version will pass the adress of a method that opens a messagebox upon error, and the console version will simply display the exception text to the console and write it to a errorlog - and maybe later send me a mail.
In this manner I don't have to change the library, and in an error condition the library calls the delegate that was passed by the calling program.
In VO I would have used a codeblock (like the one that is set with the ErrorBlock() function, but a delegate is a much better choice, I think.
But maybe my thinking is just too complicated.....

Wolfgang

Exception handling

Posted: Tue Sep 10, 2019 3:14 pm
by Chris
Hi Wolfgang,

OK, I understand, the delegate sounds good. Maybe I would just change the declaration to an event, to make it probably more readable:

Code: Select all

EVENT ExceptionHappened AS ExceptionHandler
The other possible way to implement this (in a strongly typed way) is to use an interface:

Code: Select all

INTERFACE ISupportsExceptionReporting
   METHOD ReportException(oException AS Exception)  AS LOGIC
END INTERFACE
then have both GUIs implement this interface and in your calling code use:

Code: Select all

LOCAL oGuiToUse AS ISupportsExceptionReporting
oGuiToUse := CONSOLE_GUI
...
oGuiToUse:ReportException(oException)
either way is fine IMO.

Exception handling

Posted: Tue Sep 10, 2019 3:39 pm
by wriedmann
Hi Chris,

I will try it ( the event thing) and let you know.

And of course the interface is another good idea.

Wolfgang