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
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.
@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?)
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.
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 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
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.
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.
@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.
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.