fbpx
Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC: Exception handling

Exception handling 1 week 23 hours ago #10664

  wriedmann's Avatar Topic Author wriedmann Away Posts: 1745
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:
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

www.riedmann.it - docs.xsharp.it

Please Log in or Create an account to join the conversation.

Last edit: by wriedmann.

Exception handling 1 week 18 hours ago #10668

  Chris's Avatar Chris Offline Posts: 1341
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?
XSharp Development Team
chris(at)xsharp.eu

Please Log in or Create an account to join the conversation.

Exception handling 1 week 18 hours ago #10669

  wriedmann's Avatar Topic Author wriedmann Away Posts: 1745
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

www.riedmann.it - docs.xsharp.it

Please Log in or Create an account to join the conversation.

Last edit: by wriedmann.

Exception handling 1 week 17 hours ago #10671

  Chris's Avatar Chris Offline Posts: 1341
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:

EVENT ExceptionHappened AS ExceptionHandler

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

INTERFACE ISupportsExceptionReporting
   METHOD ReportException(oException AS Exception)  AS LOGIC
END INTERFACE

then have both GUIs implement this interface and in your calling code use:

LOCAL oGuiToUse AS ISupportsExceptionReporting
oGuiToUse := CONSOLE_GUI
...
oGuiToUse:ReportException(oException)

either way is fine IMO.
XSharp Development Team
chris(at)xsharp.eu

Please Log in or Create an account to join the conversation.

Last edit: by Chris.

Exception handling 1 week 17 hours ago #10672

  wriedmann's Avatar Topic Author wriedmann Away Posts: 1745
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

www.riedmann.it - docs.xsharp.it

Please Log in or Create an account to join the conversation.

  • Page:
  • 1