Wish Catch Try Insert around existing code, instead of at top/insert point

JohnK

Active Member
Licensed User
Longtime User
Just a small idea, but, I don't think I am the only one, but I have a habit of adding my error handlers after the majority of code is written.

Currently when I go to insert a try, B4A automatically inserts the whole
B4X:
try
catch
    Log(....)
end try
Block, right under where I have entered the "try" statement and press return, and hence I have to move the catch-end try to the bottom, and then tab/format the code in the sub manually

It would be handy if I was to enter "try" at the start of a sub and press enter, it would add the catch-"end try" at the bottom of the same sub, and indent all the code in the sub to be one tab indented as compared to the try-catch-"end try"

But, maybe its just me...
 

JohnK

Active Member
Licensed User
Longtime User
I just finished adding a bulk of error handling to a current app, so the job is done on that one. I'll give this a go on my next attempt.

Just tabbed and tested in B4A, and there is still the extra step of selecting the code and indenting it. It is a slight improvement, but still I think would be better if it did all the steps automatically. I guess the question is why would you not want it to do it all?

Your gif doesn't show that I had to manually select the original code and indent it?

BTW, I don't recommend overusing Try / Catch. It will lead to hard to discover bugs.
That's a personal preference and method. ie Ask 1000 programmers the same question you will receive 1000 valid answers. I personally find the way I use it makes finding the source of bugs/errors down to a pinpoint very handy; with stack traces back to the original source throw location. It is to some extent a personal preference and mode of operation.


On a side...
Once you get used to moving lines with Alt + Up / Down
Whoa, I use a two screen setup and just found a new hot-key (ctrl-alt-up/down) combination for turning one of the screens upside down! making it hard to get back to normal.
 

JohnK

Active Member
Licensed User
Longtime User
The gif was not edited
The key press didn't come through in the gif

Click on Alt + F to indent the sub. Do it when nothing is selected. It will then indent the complete sub.
Although it is better (no knowing the hot-key), I still see it as a "wish", and the only reason against including the feature I can think of, is that the one sub may have multiple catch try block in it ie defaulting to wrapping the whole may be overkill. Apart from time-cost-priority, is there a specific reason someone would not want the steps you perform above, to be automatically done every time?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Apart from time-cost-priority, is there a specific reason someone would not want the steps you perform above, to be automatically done every time?
1. time-cost-priority is of course very important.
2. I don't think that adding Try / Catch blocks to complete subs is a good practice. I think that it is a mistake.
3. It is already very simple to add Try / Catch blocks to complete subs.
 

JohnK

Active Member
Licensed User
Longtime User
I think the total lack of interest shows that it is obviously only me, and basically answers all the questions. Thanks for the consideration and time.
Although a cry emoji would be good in the forum
 

Peter Simpson

Expert
Licensed User
Longtime User
I don't think that adding Try / Catch blocks to complete subs is a good practice. I think that it is a mistake.

It's a terrible practice. I only ever use Try Catch if there is actually an issue @JohnK, and then only temporarily until I fix the issue at hand. It's extremely rare for me to leave Try Catch in my app not unless I really have to and I have absolutely no other choice but to do so.
 

jimmyF

Active Member
Licensed User
Longtime User
It's sort of like "On Error Resume Next"

Great if you like surprise endings!
 

JohnK

Active Member
Licensed User
Longtime User
It's sort of like "On Error Resume Next"

Great if you like surprise endings!
Its the complete opposite of a resume next when used appropriately. Historically, some older prog languages were based purely on "resume next" style error handling. and to be clear, try-catch-throw is not a resume next when coded with thought.
Its all coding style, "terrible practice" is a personal opinion based on how you have seen it used. The tool is there and if used appropriately its a very very powerful tool. I too have seen it used poorly.
 

JohnK

Active Member
Licensed User
Longtime User
Actually realised after I walked away from keyboard, the big disconnect is the THROW part of a try-catch-throw
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…