Question about error handling

I’m trying to learn the error handling system in Xojo and have a question.

I was trying to open and write to a file and received e.ErrorNumber of “2” which I looked up and found meant “ENOENT 2 No such file or directory” but the e.Reason and e.Message properties return nothing.

' do something to generate an error here

Exception e As RuntimeException

MsgBox(e.ErrorNumber.ToText)
MsgBox(e.Reason)
MsgBox(e.Message)

Is it normal for the e.Reason and e.Message to return nothing? Shouldn’t they return a textual description of the error?
Is this a problem with Xojo on the Raspberry Pi?
Am I doing something wrong?

-Wes

Hi Wes,

It’s not mandatory to display a message at all. Nevertheless, you can give more info to you app raising your own exceptions.

Try this in first place (for example in the Action event of a button):

[code]Dim f As FolderItem = Nil

Try

Dim tof As TextOutputStream = TextOutputStream.Create(f)

tof.WriteLine( “hola caracola” )

End Try

Exception e As NilObjectException

MsgBox e.ErrorNumber.ToText + EndOfLine + e.Message + EndOfLine + e.Reason[/code]

Now try this. Here you are “catching” the possible error, creating your own Exception and giving more meaningful information to it:

[code]Dim f As FolderItem = Nil

Try

Dim tof As TextOutputStream = TextOutputStream.Create(f)

tof.WriteLine( “hola caracola” )

Catch

Dim exc As New NilObjectException

exc.ErrorNumber = 100
exc.Message = “Something went wrong!”
exc.Reason = “The file was pointing to nil.”

Raise exc

End Try

Exception e As NilObjectException

MsgBox e.ErrorNumber.ToText + EndOfLine + e.Message + EndOfLine + e.Reason[/code]

As you can see, the information put into the “Message” property has no effect. So you can use this (or your own subclasses) in your app to propagate the exceptions, handling them from the caller or the “UnhandledException” event, for example.

Javier

1 Like