Error handler for ported VO applications

This forum is meant for examples of X# code.

User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Error handler for ported VO applications

Post by wriedmann »

Hi,

if you move your VO application to X#, unfortunately your error handler will not more work.

Therefore you should change your Start module as follows:

Code: Select all

function Start() as int
  local oXApp as XApp

  AppDomain.CurrentDomain:UnhandledException += VOXSErrorHandler.UnhandledException
  try
    oXApp := XApp{}
    oXApp:Start()
  catch oException as Exception
    VOXSErrorHandler.ProcessException( oException )
  end try

  return 0
And this is my error handler (also attached as prg file to this message):

Code: Select all

using System.Text  
using System.IO    
using System.Reflection

static class VOXSErrorHandler
	
static constructor()
	
  return
	
static method UnhandledException( Sender as object, e as UnhandledExceptionEventArgs ) as void
		
  VOXSErrorHandler.ProcessException( ( Exception ) e:ExceptionObject )
			
  return

static method ProcessException( oException as Exception ) as void
  local cMessage as string
	
  cMessage := oException:Message
  VOXSErrorHandler.WriteErrorLog( oException )
  do while oException:InnerException != NULL_OBJECT
    oException := oException:InnerException
    VOXSErrorHandler.WriteErrorLog( oException )
    cMessage += CRLF + oException:Message
  enddo
  VOXSErrorHandler.ErrBox( cMessage )
	
  return
	
static method WriteErrorLog( oException as Exception ) as void
  local cFileName as string
  local cErrorText as string
  local oSB as StringBuilder

  cFileName := Path.Combine( AppDomain.CurrentDomain:BaseDirectory, "XSharpError.log" )
  oSB := StringBuilder{ String.Format( "Error occurred in {0} at {1}", ;
    Assembly.GetEntryAssembly():Location, DateTime.Now:ToString() ) }
  oSB:AppendLine( "------------------------------------------------------------" )
  oSB:AppendLine( oException:Message )
  oSB:AppendLine( "Callstack:" )
  oSB:AppendLine( oException:StackTrace )
  oSB:AppendLine( "" )
  File.AppendAllText( cFileName, oSB:ToString() )
	
  return

static method ErrBox( cText as string ) as void
  local oParent	as object
  local oBox as TextBox

  oParent := GetObjectByHandle( GetActiveWindow() )
  if oParent == null_object
    oParent := nil
  endif
  oBox := TextBox{ oParent, "Error", cText }
  oBox:Type := BOXICONHAND
  oBox:Beep := true
  oBox:Show()

  return

end class


Hope this helps someone!

Wolfgang
Attachments
VOXSErrorHandler.zip
(912 Bytes) Downloaded 70 times
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
FFF
Posts: 1580
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

Error handler for ported VO applications

Post by FFF »

Wolfgang Riedmann wrote:

Code: Select all

	
  oParent := GetObjectByHandle( GetActiveWindow() )
  if oParent == null_object
    oParent := nil
Wolfgang,
thx for that.
I try to understand what i use, so, a word to the above lines -> Null_Object vs. Nil ?

TIA
Karl
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Error handler for ported VO applications

Post by wriedmann »

hi Karl,
Answering from my phone.
In the MessageBox class there is a check about the owner beimg nil, and AFAIK in the VO runtime there is a difference between nil and null_object.
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
FFF
Posts: 1580
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

Error handler for ported VO applications

Post by FFF »

Wolfgang,
that was in my mind, too. But a quick try in 2838 a la
LOCAL oParent as OBJECT
// IF oParent == null_object
? oParent := null_object, "Null_Obj"
// end
? oParent == nil, "nil"
showed no difference, regardless of how i construct the compare.

Therefore my question...
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Error handler for ported VO applications

Post by wriedmann »

Hi Karl,

this is the code from the GUI classes:

Code: Select all

	IF !IsNil(uParent)
		IF !IsInstanceOfUsual(uParent, #Window)
			WCError{#Init,#TextBox,__WCSTypeError,uParent,1}:@@Throw()
		ELSE
			oParent := uParent
		ENDIF
	ENDIF
Therefore I have the NIL in my VO code, and copied from there to the error handler. This is very old code, was written at the times of VO 1.0.
But of course you can change it as you like.

My standard error handler in VO has also a print button, and the error text is shown in a MultiLineEdit. I have yet to construct such a dialog for X#, and of course there will 3 different windows: VO GUI, WinForms and WPF.

This is only a working starting point - without such a handler it may be harder to find errors - therefore it writes also an error log in the program directory (I don't install my software in the "Program Files" structure because I'm using online updates that replace simply the executable and other files like the dictionary.

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Juraj
Posts: 161
Joined: Mon Jan 09, 2017 7:00 am

Error handler for ported VO applications

Post by Juraj »

Hi Wolfgang,

He would use a similar errorhandler in the WPF new app in Xsharp?

Juraj
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Error handler for ported VO applications

Post by wriedmann »

Hi Juraj,

the WPF errorhandler differs in the mode it displays the error.

With the VO GUI classes, you can use TextBox class to display the error message since it is a part of the VO GUI classes.
With WPF you should use a WPF window or, if you are using MVVM, a different possibility to show the error.

Therefore I have 3 different error handlers:
- for VO GUI
- for WinForms
- for WPF

In my applications, all 3 use the same error logging routine (write to a file).

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Error handler for ported VO applications

Post by wriedmann »

Hi Juraj,

I would like to point you also to this message here in the forum:

https://www.xsharp.eu/forum/public-vo-vn/310-suggestions-for-conversion-vo-programs-using-vo-gui-classes?start=10#1984

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Juraj
Posts: 161
Joined: Mon Jan 09, 2017 7:00 am

Error handler for ported VO applications

Post by Juraj »

Hi Wolfgang,

Thank you for your response. You could send me an example as you do in the WPF app?

Juraj
User avatar
wriedmann
Posts: 3755
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Error handler for ported VO applications

Post by wriedmann »

Hi Juraj,

I have to extract the sample from my libraries.

I will post a sample application tomorrow (I'm working out of office today until late).

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Post Reply