Exception handling

This forum is meant for anything you would like to share with other visitors
Post Reply
User avatar
wriedmann
Posts: 3644
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Exception handling

Post 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
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
Chris
Posts: 4562
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Exception handling

Post 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?
Chris Pyrgas

XSharp Development Team test
chris(at)xsharp.eu
User avatar
wriedmann
Posts: 3644
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Exception handling

Post 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
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
Chris
Posts: 4562
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Exception handling

Post 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.
Chris Pyrgas

XSharp Development Team test
chris(at)xsharp.eu
User avatar
wriedmann
Posts: 3644
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Exception handling

Post 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
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Post Reply