Android Question Try/ Catch with CallSub Difference between Debug and Release/ Release(obfuscated)

RJB

Active Member
Licensed User
Longtime User
Using this test code in a class:
B4X:
public Sub TestCall(TestSubString As String)
Try
     Log("Calling sub in Main")
     CallSub(Main, TestSubString & "_Data")
     Log("Calling sub in Class")
     CallSub(Me, TestSubString & "_Data")
Catch
     Log("Catch")
     Log(LastException)
End Try
End Sub
Sub Refresh_Data
      Log("------------> Class Refresh data")
End Sub
everything works fine when the two called subs exist.
If the sub in Main doesn't exist then it is ignored (no action taken). This is so in debug, release and release(obfuscated) modes. That's fine, I can work with that.
However if the sub in the class doesn't exist there is a problem:
In debug the code works as expected and the lines in 'catch' are executed.
However in release and release(obfuscated) the program crashes:
B4X:
Calling sub in Class
test_v5 (java line: 51)
java.lang.Exception: Sub refresh_data was not found.
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:202)
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:1083)
at anywheresoftware.b4a.keywords.Common.CallSubNew(Common.java:1030)
at b4a.example.test._v5(test.java:51)
at b4a.example.main._b_click(main.java:369)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:180)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:6311)
at android.view.View$PerformClick.run(View.java:24996)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6537)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:857)
Is there any way to handle this situation without the crash? Note: the called sub doesn't exist so it can't be handled there.
It's not practical to " just make sure the sub does exist" due to the way the real App works.
Thanks
 

RJB

Active Member
Licensed User
Longtime User
Yes thanks that works. I missed that and in all of the searching here I didn't find it.
Hopefully your reply will help others when they search now.
Out of curiosity I'd still be interested in why Debug and Release modes work differently though, if anyone knows.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
There is an edge case here which I'm not sure that is dealt in the best way internally.

The difference in the behavior is related to Application_Error which is only called in release mode. It will not crash if you return False from Application_Error.
The correct solution is to use SubExists or XUI.SubExists.
 
Reactions: RJB
Upvote 0

RJB

Active Member
Licensed User
Longtime User
OK thanks. I'm using SubExists and it seems to be working fine. I'll try False from Application_Error as well.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…