Android Question Exceptions and ResumableSub

Creideiki

Active Member
Licensed User
Longtime User
Is it possible to handle an exception thrown in an resumable sub?

Example:
B4X:
Sub throwAndResumableSubTest
   Log("Aufrufer startet")
   Try
       Wait For (resumableAndThrowing) Complete
   Catch
       Log(LastException)
   End Try
   Log("Aufrufer endet")
End Sub

Sub resumableAndThrowing As ResumableSub
   Log("ResumableSub startet")
   Sleep(100)
   Dim ex As ExceptionEx
   ex.Initialize("Meine eigene Ausnahme.")
   ex.Throw
   Log("ResumableSub endet")
   Return 0
End Sub

Since the second sub already returned when the exeption is thrown, the try...catch block can't handle it. The app crashes.

Is it possible to handle something like that?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Note that the Complete event should include the result parameter:
B4X:
Wait For (resumableAndThrowing) Complete (Unused As Int)

You will need to catch the error inside the resumable sub. You can change your code to:
B4X:
Sub throwAndResumableSubTest
   Log("Aufrufer startet")
       Wait For (resumableAndThrowing) Complete (Success As Boolean)
   Log("Aufrufer endet")
End Sub

Sub resumableAndThrowing As ResumableSub
   Try
   Log("ResumableSub startet")
   Sleep(100)
   Dim ex As ExceptionEx
   ex.Initialize("Meine eigene Ausnahme.")
   ex.Throw
   Catch
  Return False
  End Try
  Log("ResumableSub endet")
   Return True
End Sub
 
Upvote 0

Creideiki

Active Member
Licensed User
Longtime User
Hm, ok... I wanted to use Exceptions for error handling, so I don't have to handle the error for each and every sub I call.
It seems I have to use another method then.

Thanks.
 
Upvote 0
Top