Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it ok to wrap the whole code into "try ... except" as the last resort?

Say, I have a big application in python running on a server a few times per day and sometimes it crashes. I want, when it crashes, it at least to release all the resources such as db connection, file handlers, etc.

I've got some "try ... except" blocks in it, but who knows where it's going to crash next? It's possible it'll crash somewhere where the code isn't wrapped into "try ... except".

What's a recommended way to improve it? Should I wrap the whole script body into "try ... except" as the last resort? Or what?

like image 712
Saurabh Avatar asked Mar 11 '23 07:03

Saurabh


1 Answers

Wrap the whole code with a try-catch is the most simple way but it doesn't fix the problem.

I think the best way is to find out the root cause of crash and take care of it even just print an error message and exit.

like image 160
Simon J. Liu Avatar answered Apr 05 '23 23:04

Simon J. Liu