Catch unhandled exceptions globally?

642 views
Skip to first unread message

BP

unread,
Mar 23, 2013, 9:10:44 PM3/23/13
to haxe...@googlegroups.com
Hey!


Is there a way in haxe to set a global exception handler? I've googled it but come up with nothing...

For example, I want to catch any unhandled errors, and break into the hxcpp debugger.... To do this, I want to have one place I can catch errors that are unexpected.

Thanks!

Jason O'Neil

unread,
Mar 23, 2013, 10:04:54 PM3/23/13
to haxe...@googlegroups.com
Could you just do:

static function main()
{
    try {
        // run your app
    }
    catch (e:Dynamic) {
        // do your debugging?
    }
}

I have no idea of the performance implications etc but that should catch all exceptions in your run of code.  Though I guess anything involving threads or async might escape...

Maybe someone has a better idea :)



--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Juraj Kirchheim

unread,
Mar 24, 2013, 7:57:21 AM3/24/13
to haxe...@googlegroups.com
Some platforms have means to register global exception handlers, but
hxcpp might not be one of them.

If this is about NME, then I suppose you can get very far by hacking a
try-catch clause into dispatchEvent since all code execution is the
result of an event. Best to hook into things here:
https://github.com/haxenme/NME/blob/master/native/events/EventDispatcher.hx#L163

You can then rethrow the exception with
cpp.Lib.rethrow/neko.Lib.rethrow if needed.

Regards,
Juraj

TopHattedCoder

unread,
Mar 24, 2013, 3:22:27 PM3/24/13
to haxe...@googlegroups.com
Perhaps you could make a macro to automatically run and rename the main method to something else then make a new wrapper main method?

BP

unread,
Mar 24, 2013, 8:21:36 PM3/24/13
to haxe...@googlegroups.com
hey! thanks for the suggestions.... I really hate to hack things in.... i'd think that there should be at least a nice NME way of doing this... even not for debugging, but for just handling unexpected situations nicely in apps, so apps at least fail nicely in error conditions.... hmmmmmm..... 

Jason O'Neil

unread,
Mar 24, 2013, 8:52:47 PM3/24/13
to haxe...@googlegroups.com

Maybe make a suggestion / feature request to the NME dev team? Juraj's suggestion sounds like it would have merit in terms of how to implement it.

https://haxenme.atlassian.net/issues/

--

BP

unread,
Mar 24, 2013, 9:49:06 PM3/24/13
to haxe...@googlegroups.com
Good idea! I've posted a feature request for it here:


I'll update this thread if I hear anything new about it... thanks!

Hugh

unread,
Mar 25, 2013, 12:29:10 AM3/25/13
to haxe...@googlegroups.com
Hi,
The haxe main is enclosed in a global try-catch.  This is what prints out the stack trace if it is not handled.
Debugging from here may not be exactly what you want, since all the local variables etc have been cleaned up by the time you get to the catch block.
You can breakpoint in "hx::Throw" (src/hx/StdLibs.cpp) but you will get all exceptions, even unexceptional ones such as stream EOF.

Normally null-access does not throw an exception, but if you use "-debug", you can breakpoint in hx::CriticalError (src/hx/Debug.cpp) to find out what is going in.

nme wraps 'nmeProcessStageEvent' in a try-catch on android only in native.display.Stage.hx. This will catch (and ignore) errors in your event handlers - almost everything is run from an event handler.  Android only, because attaching a real debugger is hard on android.  But you could increase this for other targets by editing the Stage.hx.  I'm not convinced that this is the best way to handle the errors because it can hide problems and if you are not setup to recover from exceptions other things will probably go wrong later.

Maybe nme could only catch if a special error handler is present, on the understanding that the app is going to finish up and exit gracefully.


Hugh

BP

unread,
Mar 25, 2013, 11:20:40 AM3/25/13
to haxe...@googlegroups.com
thanks for the suggestions.... it sounds like hx::CriticalError will work for debugging purposes for right now.... thanks...
Message has been deleted

Samuel Batista

unread,
Mar 25, 2013, 12:33:26 PM3/25/13
to haxe...@googlegroups.com
Not having a visual debugger for hxcpp to catch these exceptions and give the programmer an overview of what caused the error by being able to look at haxe source in their IDE of choice is a big missing piece of the Haxe workflow. From what I saw of the hxcpp debugging all features are present on the hxcpp side, it's FlashDevelop that is lacking developer support.

I'm interested to find out how to mobilize the haxe community to make directed efforts at fixing critical pipeline flaws. I wonder if the HaxeFoundation could provide a way to leverage crowd funding to provide monetary incentives to FlashDevelop project or other individual haxe community members in order to rally the community behind big problems like this one.

It's an interesting thought, but open source projects that have financial backing (whether through private contributions, or crowd support), seem to evolve much more quickly and stick around for much longer.

BP

unread,
Mar 25, 2013, 8:17:04 PM3/25/13
to haxe...@googlegroups.com
I completely agree that a visual debugger is a missing and important piece of the Haxe puzzle. I think that many people would get to this point, and would just switch back to AS3, as they could at least debug there. 

I wonder if a KickStarter that was started by Joshua or Hugh for integrating visual debugging with some popular opensource IDE's (FlashDevelop, MonoDevelop) would be a good idea? I'd fund it for sure. I know anyone can start a Kickstarter, but it's usually having the right people associated with it that get a project to go to the funded step....

Chris D

unread,
Mar 25, 2013, 10:30:57 PM3/25/13
to haxe...@googlegroups.com
I know its been said before, but a cross platform Haxe Foundation IDE project would be a huge benefit to the Haxe ecosystem, hopefully higher teir sponsors can help? It obviously requires some specialized talent and full time work. Its a huge tooling thing the "similar" Dart language http://www.dartlang.org/docs/editor/ has over Haxe. I think this is significant with the future Haxe adoption over the next few years, especially if Haxe is promoting it's js output and web technologies where developer's are used to having a vast choice of IDE's and tools. I am pretty sure a lot of developers will choose something with the best workflow with advanced IDE integration / debugging support, even if it is an inferior technology.

For anyone reading, Intellij Idea was able to offer support with hxcpp debugging thanks to Hugh's work, although its a wip and it doesn't yet support all Haxe language features with it's parser/lexer and bugs with handling of macros (you cant copy/paste them). Its a shame their Dart plugin has seen more updates in contrast recently, but I am told as they have also been busy with other recent product releases. Haxe 3 support should come soon since it currently doesn't recognize the Std Types due to the new abstracts, I have high hopes for it as a more stable commercial Haxe IDE as its such a wonderful and powerful java/as3 IDE and worth every cent for those targets *crossing fingers.
Reply all
Reply to author
Forward
0 new messages