Android Question Does exceptions in inline java mess with live updating the code?

Discussion in 'Android Questions' started by Sandman, Oct 21, 2018.

  1. Sandman

    Sandman Well-Known Member Licensed User

    I'm testing some things and have a piece of inline java that is throwing an exception under some conditions. That exception is not fatal for the app at all, and is also wrapped in a try/catch block.

    However, if the exception is thrown, it's not possible to live update the code anymore. In fact, one needs to even clean the project to get it running as expected again.

    I can't tell if this is a bug, a known limitation or simply an error on my part somewhere. I'm hoping for the latter, and for the forum to help me out. :)

    This is my code. (Stripped down to bare minimum.)

    Code:
    Sub Globals
        
    Dim myButton As Button
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
        myButton.Initialize(
    "myButton")
        myButton.Text = 
    "Throw exception"
        
    Activity.AddView(myButton, 5%X30%Y,90%X,20%Y)
    End Sub

    Sub myButton_Click

        
    Log("Clicked")

        
    Try
            
    Dim JO As JavaObject = Me
            JO.RunMethod(
    "justTestingStuff"Null' <-- This is line 44
        Catch
            
    Log(LastException)
            
    Return
        
    End Try

        LogColor(
    "Everything seems to have worked out fine."Colors.Green)

    End Sub

    #if JAVA
    public static void justTestingStuff() { 
        throw new RuntimeException("Stuff went bad");
    }
    #End If

    A tap on the button produces this in the log...

    Code:
    Clicked
    (RuntimeException) java.lang.RuntimeException: Stuff went bad
    ...which seems to be working perfectly fine. The exception message is in there. Multiple taps just produces the same over and over again.

    However, any code changes in the myButton_Click sub changes things completely - even if the change is totally harmless. (It seems to be fine to change other subs though.) To try this, change the log message "Clicked" to "Clicked2" and save to update the code running in the phone.

    Tap the button, and this is the result:

    Code:
    Clicked2
    Error occurred on line: 
    44 (Main)
    java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at anywheresoftware.b4j.object.JavaObject.RunMethod(
    JavaObject.java:131)
        at java.lang.reflect.Method.invoke(Native Method)
        at anywheresoftware.b4a.shell.Shell.runVoidMethod(
    Shell.java:778)
        at anywheresoftware.b4a.shell.Shell.raiseEventImpl(
    Shell.java:358)
        at anywheresoftware.b4a.shell.Shell.raiseEvent(
    Shell.java:255)
        at java.lang.reflect.Method.invoke(Native Method)
        at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:
    144)
        at anywheresoftware.b4a.BA.raiseEvent2(BA.java:
    175)
        at anywheresoftware.b4a.BA.raiseEvent(BA.java:
    171)
        at anywheresoftware.b4a.objects.ViewWrapper$
    1.onClick(ViewWrapper.java:80)
        at android.view.View.performClick(
    View.java:6294)
        at android.view.View$PerformClick.run(
    View.java:24770)
        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:
    6494)
        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:
    807)
    Caused by: java.lang.RuntimeException: Stuff went bad
        at b4a.example.main.justTestingStuff(main.java:
    457)
        ... 
    20 more
    (
    Exception) java.lang.Exception:  java.lang.reflect.InvocationTargetException
     
  2. Semen Matusovskiy

    Semen Matusovskiy Well-Known Member Licensed User

    I tried to change Log ("Clicked") to Log ("Clicked2") in all regimes (Release, Debug, Obfuscated). The output is expected
    Clicked2
    (RuntimeException) java.lang.RuntimeException: Stuff went bad


    I also tried to change a text of the button. Also all is expected.
    So, the problem does not exist or you need to describe a situation more exactly.

     
  3. Erel

    Erel Administrator Staff Member Licensed User

    It is a bit confusing but everything is working properly. The exception is caught.

    The only difference that the full stack trace is logged. This is how exceptions look when the code is executed by the slower and smarter pipeline. It will also happen if you put a breakpoint in that sub.
     
  4. Sandman

    Sandman Well-Known Member Licensed User

    Yes, that is true. But the type of the exception is changed. In the first case GetType(LastException) returns java.lang.RuntimeException, and after the edit that changes to java.lang.Exception.

    I have no problem with the log getting a stack trace all of a sudden, but it's a little bit problematic that the type changes. Is this something even worth posting a wish about, or is it way off the radar in the cost/benefit ratio?

    (Why am I interested in this? I'm strongly considering breaking up my app in a bunch of libraries with super clean separations. And using custom (not shown in the code above) exceptions is a nice way of communicating fails back to the main app, which will use try/catch blocks for all calls into my libs. And I can't really do that if the type of the exception changes. That said, we're talking about scenarios where I'm live updating code, so it's not the end of the world if it doesn't work. It just adds some friction.)
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    Posting a feature request never does harm. It is unlikely to change in the near future as it only affects the debugger and it is considered a minor issue.
     
  6. Sandman

    Sandman Well-Known Member Licensed User

    Are you using the legacy debugger, or something like that? It seems strange that you're not seeing this problem. Can you try the attached project? And perhaps attach the one you did also?
     

    Attached Files:

  7. Sandman

    Sandman Well-Known Member Licensed User

  8. Semen Matusovskiy

    Semen Matusovskiy Well-Known Member Licensed User

    Today I, at first, took your zip, then attached. Tested under API 25 emulator in release mode .
    Should be explanation. First of all, do you use B4A 8.50 ?
     

    Attached Files:

    Last edited: Oct 22, 2018
  9. Sandman

    Sandman Well-Known Member Licensed User

    I feel we have a little bit of language barrier here, but are you saying you tried it in release mode? That's not what this thread is about. This is only about running the app in debug mode, changing the code, saving and continue running - without stopping the app in between.


    I sure do.
     
  10. Semen Matusovskiy

    Semen Matusovskiy Well-Known Member Licensed User

    Ah, I see a problem.
    But just curious - do you see advantages to use debugger instead of Log ?
     
  11. Sandman

    Sandman Well-Known Member Licensed User

    And what problem is that?


    I'm not sure I understand the question. It's quite common for me that I'm running the app in debug mode, while also using Log command, when I'm coding things. You don't do this? You compile a full release every time?
     
  12. Semen Matusovskiy

    Semen Matusovskiy Well-Known Member Licensed User

    The problem is exactly like you described. See below.

    Personally I have serious prejudice agaist debuggers.
    It was a period when I used Visual Basic. Thanks to interpreter to debug was enough comfortable.

    But then I began to use PowerBasic (www.powerbasic.com). Fantastic compiler, but not comfortable debugger.
    Because I continue to use PowerBasic in Windows, I subconsciously transfer hostility to B4A debugger.
    For me Log statement is more than enough :)


    Code:
    Clicked2
    Error occurred on line: 
    42 (Main)
    java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at anywheresoftware.b4j.object.JavaObject.RunMethod(
    JavaObject.java:131)
        at java.lang.reflect.Method.invoke(Native Method)
        at anywheresoftware.b4a.shell.Shell.runVoidMethod(
    Shell.java:783)
        at anywheresoftware.b4a.shell.Shell.raiseEventImpl(
    Shell.java:363)
        at anywheresoftware.b4a.shell.Shell.raiseEvent(
    Shell.java:260)
        at java.lang.reflect.Method.invoke(Native Method)
        at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:
    144)
        at anywheresoftware.b4a.BA.raiseEvent2(BA.java:
    175)
        at anywheresoftware.b4a.BA.raiseEvent(BA.java:
    171)
        at anywheresoftware.b4a.objects.ViewWrapper$
    1.onClick(ViewWrapper.java:80)
        at android.view.View.performClick(
    View.java:5610)
        at android.view.View$PerformClick.run(
    View.java:22265)
        at android.os.Handler.handleCallback(Handler.java:
    751)
        at android.os.Handler.dispatchMessage(Handler.java:
    95)
        at android.os.Looper.loop(Looper.java:
    154)
        at android.app.ActivityThread.main(ActivityThread.java:
    6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:
    866)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:
    756)
    Caused by: java.lang.RuntimeException: Stuff went bad
        at b4a.example.main.justTestingStuff(main.java:
    451)
        ... 
    20 more
    (
    Exception) java.lang.Exception:  java.lang.reflect.InvocationTargetException
    java.lang.Exception
     
  13. Sandman

    Sandman Well-Known Member Licensed User

  14. Erel

    Erel Administrator Staff Member Licensed User

    This is not the topic of this thread, however you should really get used to running in debug mode. Even just for the rapid compilation feature.
     
    Sandman likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice