Error.Message does not show the message in messagebox

Window 11 using Xojo 2024r4.1
In a webapp, unhandledexeptions, I have the following code. It does Not show the error.message in the Messagebox. It only shows, An unexpected error occurred:. The error.message does show in the DebugLog. Any idea what is causing this?

// Log the error when in debug mode
System.DebugLog("Unhandled Exception: " + error.Message)

// Display an error message using MessageBox
MessageBox("An unexpected error occurred: " + error.Message)

// Return True to indicate the exception has been handled
Return True

Maybe too obvious of a question, but is there a session context when this code is being called?

Cheers,
Anthony

Yes, the code is in the Session.unhandledexception event handler.
How should I inform the user that an unhandled exception occured along with the reason it occured?

I am of the belief that an UnhandledException should end the application. It’s an exceptional case that hasn’t been handled, you never know what trouble it causes.

Personally, I Print() everything to the log that Lifeboat reads and force an exit (I’ve found return false doesn’t work as it does on Desktop). Lifeboat then restarts the app. You can take a look at how I handle UnhandledExceptions in this sample project: https://tim.gd/uzwDJ

I am not sure why you’re seeing such a weird String truncation behavior.

2 Likes

@Gary_Smith - I misunderstood your original post. (I thought you were saying the MessageBox wasn’t being shown at all in the browser, vs. the ‘message’ property was empty)

I made a simple app, and I too have no data in the ‘message’ property of the error event parameter.

I see the same issue (RuntimeError object passed in error param doesn’t seem to have the actual error info) in both 2024r4.1 and 2025r1.1.

This is true both on my Mac while debugging and with a compiled build on Windows 2019 Server.

The error code also looks incorrect? (zero feels like the default value?)

msgtest.xojo_binary_project.zip (8.0 KB)

An OutOfBoundsException has no message from the framework. Compare the way you’re generating the exception to executing something like this:

raise new OutOfBoundsException( "Has message content." )

Some exceptions like NilObjectExceptions have no info.

You can create a global method that extends runtimeexception

Function DebugMessage(extends e aa runtimeexception) As String

Select case e

Case isa nilobjectexception 
Return "Some object was nil"

Case IsA OutOfBoundsException
Return "Some value was out of bounds"

Else 

Return e.message
End Select

End Function

Use it as:
MessageBox( "An error was detected: " + e.DebugMessage)

Edit: Added the OutOfBoundsException as @Anthony_G_Cyphers suggested above.

@Gary_Smith - based on what @Anthony_G_Cyphers and @DerkJ are saying, it’s normal behavior for some errors to not have any information.

I like @DerkJ 's suggestion of extending RuntimeException so you can make sure you get a more meaningful information. (I do something similar in my production apps, which is probably why I expected data to be available; I’d forgotten some exceptions don’t have data available.

I’ve updated the example project.

msgtest.xojo_binary_project.zip (8.4 KB)

1 Like

I typically use introspection to get the type of the exception, and some code that looks, generally, like this:

Function UnhandledException(error As RuntimeException) Handles UnhandledException as Boolean
  var info as Introspection.TypeInfo = Introspection.GetType(error)
  
  var output() as String
  output.Add( "An error occured." )
  output.Add( "Type: " + info.Name )
  output.Add( "Number: " + error.ErrorNumber.ToString )
  if error.Message <> "" then output.Add( "Message: " + error.Message )
  
  var stack() as String = error.Stack
  if error.Stack.LastIndex > 5 then error.Stack.ResizeTo( 5 )
  output.Add( "Stack: " + String.FromArray( stack, EndOfLine ) )
  
  MessageBox( String.FromArray( output, EndOfLine ) )
End Function
1 Like

Mine does have a message and that message does show in the system.debuglog.
It just does not show that error.message in the messagdialog.

Is the message generated by the framework, or something you’re doing, such as:

raise new RuntimeException( "Error message here" )

If it’s the latter, I’d inspect the string in the debugger and look for characters that shouldn’t be there, like chr(0).

1 Like

The message is generated by the framework.
If I just use messagebox(error.message) it displays a blank messagebox.
If I put system.debuglog(error.message) then I can see the message in the debugger.

Maybe you could give us an example project? We don’t even know what kind of exception this is, but I (and, apparently, others) am not seeing the failure.

What type of RuntimeException is it @Gary_Smith ?

(from the list here: Exceptions — Xojo documentation)

It’s also worth pointing out that Session.UnhandledException could also fire in response to code that is not a result of a user event… like a Server Side WebTimer or WebThread. In that case, you’d be showing an error to the user that’s purely a server problem and not a user problem.

For context, the Session.UnhandledException method is called as a result of a Try-Catch that’s wrapped around the event handler call that calls your code on the current user’s session. It is not possible to hit this method if the session is Nil because that would mean that there’s no instance to call the method on.

If Session is Nil or you return False, App.UnhandledException should fire.

UnsupportedFormatException
The folder did not exist.

1 Like

@Gary_Smith - I’m assuming you’re setting a variable of type FolderItem? Safe assumption?

And since this is a web project, is this related to a file which should have been uploaded by a user, or, a pre-existing resource (some file) being accessed from the server based on a user action?

I’m wondering if file system operations (which definitely happen on the server) might not have a session context even if they’re initiated by a user action…

Context/overview of what you’re trying to do here is very likely, relevant.

Anthony

As long as they’re not asynchronous with a callback, it shouldn’t matter.

1 Like

It does not seem to matter what caused the error. The session.unhandledexception event handler will not show the error.message in a messagebox. The messagebox does pop up but no text in it. I am guessing that you can’t display the message, but can add it to a log file.
This a brand new app and I missed a Try/catch when setting a folderitem and the folder does not exist.

I’ll try to recreate in an as small as possible test app if time permits. If I do, I’ll post an updated project here.

Anthony

1 Like