Maybe you don't understand the problem. It is not a question WHERE the exception is raised, but whether it should be CAUGHT. After the Try line, all exceptions in the code that executes before Catch line should be caught, whether they are in that function or others that get called from within that block. It works as it should if the function is called directly (the exception is caught and the execution immediately goes to the statement after the Catch), but if it is called using CallSub, and the exception is raised in that function, it is NOT caught by the Try/Catch block. You can try it with this code:
Sub Activity_Resume
Try
Log("Before Call")
'CallSub("","GenerateError")
GenerateError
Log("After Call")
Catch
Log("Caught!")
End Try
End Sub
Sub GenerateError
Log("Before Error")
Dim i As Int = ""
Log("After Error")
End Sub
If you execute this code (with call to GenerateError enabled and CallSub commented out), you will get in the Log:
Before Call
Before Error
Cought!
That looks just right (the exception is rased in the GenerateError function, but caught in the Activity_Resume, as it should). Now, comment out GenerateError call and uncomment CallSub call. The Log, after execution, is:
Before Call
Before Error
An error occured....
So, the Catch/EndTry block was NOT executed. The behaviour is different.
Here is my take on it: the CallSub maybe does not really call anything, it just puts the name of the function on the stack and lets the OS call it when it wants, so it is really detached from the linear execution of the rest of the program. But in that case, shouldn't the Activity_Resume finish first, i.e. should we not see "After Call" in the log before the line "Before Error"? If everything is executed in the same thread and, therefore, in known order, why is Try/Catch ignored? Is CallSub an exception to which Try/Catch does not apply? That is my question. If that is so and must be so, is there a method to create a program-wide place where all exceptions will be handled instead of the default behaviour of displaying it to the user and terminating the program, or putting a separate exception handler in every function that is called by CallSub?