Android Question Problem with Try Catch and Wait for

davemorris

Active Member
Licensed User
Longtime User
Hi, Guys

I appear to have a problem when I insert a resummable sub, within a try/catch block i.e.

Problem with wait for:
' This will work Ok if the is error in DoSomething() - '
Try
    a = DoSomething(...)

Catch
    Log(LastException) ' Catch works and traps the error
End Try

' However if DoSomething() returns resummable and an error occurs in DoSomething()
'  then and exception is throw in the DoSomething() sub and the App halts with an exception report. i.e.
Try
    wait for (DoSomething(...)) complete( a as int) ' Exception thrown by DoSomething() and App halts.
    
Catch
    Log(LastException)    ' This does not catch the exception in DoSomething().
End Try

Could be doing something wrong (which for me is very likely) , but I have tried it with some test code and it Try/Catch does not work with Wait for.

Any comments please.

Kind regards
Dave
 

davemorris

Active Member
Licensed User
Longtime User
Hi LucaMs

Thanks for the quick response - it does work moving Try-Catch to DoSomething()

But the B4X documentation says that anything between the Try and Catch is caught by the Catch code. I could find nothing about problems , or to be careful with, using it with a "Wait for" statement.

I just wanted to be sure, and obviously advise other B4X users.

Kind regards
Dave
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
No, it won't catch it after the Wait. Remember that a Wait or Sleep causes an immediate return from the calling Sub so the code in DoSomething is not running within the call stack of the calling Sub. The calling Sub will only be re-scheduled from the message loop after DoSomething has completed and caused the error which is caught (or not!) elsewhere.
 
Upvote 0
Top