Catching Native (eg. Android) Exceptions

904 views
Skip to first unread message

Ashiq A.

unread,
Sep 7, 2012, 4:00:39 PM9/7/12
to haxe...@googlegroups.com
All,

I'm curious to know how I can trap and catch any exception, even one generated by the native platform (eg. Android). I would like to at least log the exception, and gracefully terminate by showing some sort of crash screen. I know with Android, every device has its own quirks and crashes.

This docs page: http://haxe.org/doc/java/native_code_advanced seems to say that Exceptions are not first-class until we get Java into Haxe. We now have Java in Haxe. So are exceptions now first-class citizens?

I assume I will have to wrap the main entry point of my application in a try/catch. But what exactly am I catching? How will it work on different platforms (Java vs. Flash vs. iOS)?

--Ashiq

Message has been deleted

Top Hatted Coder

unread,
Sep 7, 2012, 5:13:04 PM9/7/12
to haxe...@googlegroups.com
Exceptions generally aren't thrown when Haxe doesn't interface with the platform directly. For example, NME cannot catch native exceptions on Android because it uses a JNI interface to bridge between C++ and Java.

Raoul Duke

unread,
Sep 7, 2012, 5:16:25 PM9/7/12
to haxe...@googlegroups.com
On Fri, Sep 7, 2012 at 2:13 PM, Top Hatted Coder
<tophatt...@gmail.com> wrote:
> Exceptions generally aren't thrown when Haxe doesn't interface with the
> platform directly. For example, NME cannot catch native exceptions on
> Android because it uses a JNI interface to bridge between C++ and Java.

i suspect it can be done, but (a) requires plenty more work and (b)
tends to not be an abstraction that can easily be done across all the
targets haxe supports in a clean consistent way at the haxe level.

cf. http://stackoverflow.com/questions/8415032/catching-exceptions-thrown-from-native-code-running-on-android

it would be nice though [some day in the future when somebody has the
interest in implementing such a thing!] if at least a string of the
exception could percolate up to haxe so it could be logged :-)

Cauê Waneck

unread,
Sep 8, 2012, 1:12:15 AM9/8/12
to haxe...@googlegroups.com
Hi Ashiq!

I'm curious to know how I can trap and catch any exception, even one generated by the native platform (eg. Android). I would like to at least log the exception, and gracefully terminate by showing some sort of crash screen. I know with Android, every device has its own quirks and crashes.

This docs page: http://haxe.org/doc/java/native_code_advanced seems to say that Exceptions are not first-class until we get Java into Haxe. We now have Java in Haxe. So are exceptions now first-class citizens?

Are you talking about NME or the Java target?
I will answer based on the Java target, I think the other replies were about NME, which I really don't know how it deals with exceptions coming from Java.

The document you've read talks about Checked Exceptions ( read about it here http://en.wikipedia.org/wiki/Exception_handling#Checked_exceptions and learn to not like it here http://radio-weblogs.com/0122027/stories/2003/04/01/JavasCheckedExceptionsWereAMistake.html :) ), not exceptions in general. With the Java target, you can throw and catch any exceptions, be it native or not. The problem is with the checked exceptions. Since Haxe doesn't have this concept (thankfully so!), you will have to rely on metadata and check it yourself. There is no other "good" way to deal with them, without making them a required language feature.
When we target java bytecode directly, though, we will be able to ignore checked exceptions correctly!

Cheers!
Cauê

Ashiq A.

unread,
Sep 8, 2012, 2:03:40 PM9/8/12
to haxe...@googlegroups.com
Hi Caue,

My question is actually about NME specifically, and is about Android and iOS (but Haxe platforms in general). My understanding currently is that I will write Haxe/NME code, which the compiler will compile into Java code, and then the JDK will transform into .class files (plus an APK file for Android from the Android SDK).

Android phones have their own quirks, so if there's a bug or incompatibility on a phone, the JVM will generate an exception (not sure if it's checked or unchecked) which will percolate all the way up and will cause a crash on Android (the usual "The application blah has stopped unexpectedly ...").

I think the core of my question comes down to two things:

1) Is it possible to wrap my application execution in a try/catch so I can catch unexpected exceptions, and terminate gracefully? What do I put in the catch block (unknown:Dynamic?)

2) What kind of exception information can I get, and how? You mentioned using metadata; I assume that means things like poking the call stack.

Regards,

--Ashiq

Hugh

unread,
Sep 10, 2012, 12:55:47 AM9/10/12
to haxe...@googlegroups.com
Hi,
You have to understand how the java an haxe interact on android. It's like this:

boots in java:  GameActivity.java
  -. Create render thread (java)
    render thread boots the haxe thread
      haxe App.main()
    render thread processes an event
      - calls into haxe thread
        haxe thread may callback into java

To catch the stuff that happens in Java (ie, when haxe in not processing events), you will need to override the java templates and handle it in there.

When the haxe thread calls back into java, via jni, java exceptions are handled via the android/JNI.cpp nme file where CheckException "catches" it and throws a haxe exception.

I guess the best solution may be to ensure we catch as much as possible in java and convert them exceptions to haxe events.

When something really bad like the haxe library can't be found happens, I'm not sure you can do much besides a "Bad install" style dialog.

Hugh

Cauê Waneck

unread,
Sep 14, 2012, 9:49:23 AM9/14/12
to haxe...@googlegroups.com
Hi Ashiq! Sorry for taking so long to answer!


2012/9/8 Ashiq A. <alibha...@gmail.com>

Hi Caue,

My question is actually about NME specifically, and is about Android and iOS (but Haxe platforms in general). My understanding currently is that I will write Haxe/NME code, which the compiler will compile into Java code, and then the JDK will transform into .class files (plus an APK file for Android from the Android SDK).

NME will actually for now use hxcpp for Android, which means that your code won't be a java class file, but will be included using NDK (http://developer.android.com/tools/sdk/ndk/index.html

This may change, though, when hxJava is able to use the hxcpp CFFI directly (http://code.google.com/p/haxe/issues/detail?id=931&colspec=ID%20Status%20Priority%20Platform%20Owner%20Summary)
Reply all
Reply to author
Forward
0 new messages