Why Try ... Catch does not crash ?

somed3v3loper

Well-Known Member
Licensed User
Longtime User
Just a curious question if the statement in Try block does raise an error which should usually make application crashes , how does Try Catch avoid it ?
I hope my question was clear :D
 

udg

Expert
Licensed User
Longtime User
Another way of explaining the same concept as @KMatle did above, could be to read the following as plain English:
B4X:
Try
   one o more statements
Catch
   react to exception
End Try
It will become something like: " try these few statements and if any trappable exception occurs catch it and properly react to it".
 

Beja

Expert
Licensed User
Longtime User
B4X:
Try
   A = 3 + 8
Catch
   MsgBox("Oops! my computer can't calculate")
End Try
 

wonder

Expert
Licensed User
Longtime User
I wonder if a SIGSEGV (segmentation fault at the native level) would be caught...
 

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
Actually, debugger helps your app to crash,

In other words, if you put a debugger stop inside a try catch block, it will crash as if it were without it.

It is very helpful because the LOG(lastexception) does not tell you the line.
 

somed3v3loper

Well-Known Member
Licensed User
Longtime User
I think my question was not clear enough for you except KMatle :)
I know how to use Try Catch and know that if statements in Try block do contain errors , app will execute statements in catch block.

The thing I did not know is why it does not crash while it tries some problematic statements .
If I got KMatle's point , the crash is just an event that get raised by (VM ?) and if you use (try catch), it executes catch block instead.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
If I got KMatle's point , the crash is just an event that get raised by (VM ?) and if you use (try catch), it executes catch block instead.
That's true. All caught exceptions are exceptions that are explicitly thrown from the internal code or by the JVM (null pointer exception for example).

Hard crashes that theoretically shouldn't happen but practically do happen from time to time will not be caught. Another type of exceptions that will not be caught are "severe exceptions" like out of memory errors or stack overflow errors.
 
Top