Exception handling without interruption

Part of my source code is nested inside try statements to handle some runtime errors, at the same time every line must be executed, even if the previous line is not executing due to a runtime error.

currently my code is as follows

try try //statement1 except end; try //statement2 except end; try //statement3 except end; finally //something end; 

I am sure that you will be mistaken, even if the final result works well, I have to do this for dozens of lines.

Is there a better way to implement this

+4
source share
1 answer

If you want each statement to be executed, you must write it the way you did. Please note that in this case try / finally may not be necessary because you are swallowing all exceptions.

However, the code looks a little strange to me. I wonder if you really need to execute each expression. Usually you write:

 try statement1; statement2; statement3; except //handle exceptions end; 

Then, if there is an exception in statement1 , the other two lines will not be executed.

However, it would be even more common not to handle exceptions at all and to allow them to float to some higher level handler. If you make ordinary logical decisions using exceptions, this will be considered bad practice.

I think it would be useful to post the part of your code that handles exceptions and some details about what exceptions you expect. Then we could give you more specific advice.

+5
source

Source: https://habr.com/ru/post/1382041/


All Articles