Hi,
I'm trying to find the best way of handling exceptions at different levels in a recursive structure. This is some cut-down sample code which includes the key problems:
This works, but I have to use a "Contains" check on the message at each level, and re-raise the exception using a new ExceptionEx object. I'd prefer to just be able to selectively either handle the exception locally (like with the "file skipped" case), or pass it back up the chain with a fixed message value.
Any ideas?
Thanks
Andrew
I'm trying to find the best way of handling exceptions at different levels in a recursive structure. This is some cut-down sample code which includes the key problems:
B4X:
Sub Activity_Create(FirstTime As Boolean)
Try
RecursiveCopy("A")
Catch
Log(LastException.Message)
If LastException.Message.Contains("Copying cancelled") Then
Msgbox("Copy operation cancelled", "Confirmation")
Else
Msgbox(LastException.Message, "Error Occurred")
End If
End Try
End Sub
Sub RecursiveCopy(sTarget As String)
Try
Dim ex As ExceptionEx
If sTarget.Length < 4 Then
RecursiveCopy("B/" & sTarget)
End If
Log("Now processing: " & sTarget)
If sTarget = "B/B/A" Then ' We want to just skip this one, but carry on processing
ex.Initialize("File Skipped")
ex.Throw
Else If sTarget = "B/A" Then ' We want to cancel at this point, but return a meaningful exception
ex.Initialize("Copying cancelled")
ex.Throw
End If
Catch
If ex.IsInitialized Then
If ex.Message = "File Skipped" Then
Log("File copy skipped as requested")
Else If ex.Message = "Copying cancelled" Then
ex.Throw ' Re-raise for handling at next level
End If
Else If LastException.Message.Contains("Copying cancelled") Then
ex.Initialize("Copying cancelled")
ex.Throw ' Re-raise for handling at next level
Else
Msgbox(LastException.Message, "Error Occurred")
End If
End Try
End Sub
This works, but I have to use a "Contains" check on the message at each level, and re-raise the exception using a new ExceptionEx object. I'd prefer to just be able to selectively either handle the exception locally (like with the "file skipped" case), or pass it back up the chain with a fixed message value.
Any ideas?
Thanks
Andrew