Android Question Try/Catch impact on App performance

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi to All
maybe this is a silly or weird question. When an App grows, it may reach a limit, either relatively to code size or to memory engagement. Moreover, when it is rather complex, unexpected exceptions may happen in many parts. I am nor used to rely on Exception management, and i normally work with the so called "defensive programming". Now I have an App with hundreds of subs, potentially giving many kinds of problems, and I am tempted to put a try/catch block in many of the subs, letting the system to catch the "bad surprises". But there is no free cheese, normally. So the question is: may the Try/catch mechanims be implemented without great impact on code performance and memory issues? (BTW,In the past, I did an experiment to put a lot of code inside a try/catch bock and the compiler simply refused to do its work. But maybe I did a wrong analysis of the situation, at that time) Hope I explained my doubts.
 

Alessandro71

Well-Known Member
Licensed User
Longtime User
Try/Catch blocks are limited in size, I don't know exacly how many statements are permitted, but I hit that limit a couple of times.
I solved the issue by splitting long code sequences in smaller Try/Catch blocks.
I don't think there is any performance hit, since the exception handling is done at the JVM level, and it is always present: Try/Catch is used to handle exceptions yourself instead of the system.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
may the Try/catch mechanims be implemented without great impact on code performance and memory issues?
Try/Catch blocks do not have any impact on the performance or memory usage. The downside of catching exceptions is that you might miss programming bugs. In many cases it is easier to debug issues when the crash happens immediately (fail fast paradigm).
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…