Python Sockets and Exceptions in General
About sockets: According to python, should i do: socket.close(0) or socket.close(1) ? (Pycharm forces me to give the 'close' function an int). About Exceptions: I want to have an except block for specific exceptions and another except block in case that the ones mentioned before didn't catch the exception. should i do like so and use 'except Exception'? i know its a bad practice, but is there another way? : try: ... except (socket.error, KeyboardInterrupt, ConnectionResetError, struct.error) as error_msg: ... except Exception: ....
9/22/2021 5:25:55 PMYahel
13 AnswersNew Answer
You can use multiple except statements for a single try statement to deal with different errors in different ways. And you can always put a general except statement at the end to catch all other errors.
Simon Sauter I think that’s to simplify things to say ”it’s always good”. There is a reason the problem is mentioned in the Zen of Pyrhon as ”Errors should never pass silently. Unless explicitly silenced.” Specially in the beginning, when you test your code. If catching all errors with just except: you will never find the error you should catch.
Hi! You should catch exeptions, by if you want to catch all, just not specify the exepton: try: …. ….. execpt ValueError: …. except: … Here is an example with multiple exceptions: https://code.sololearn.com/cOL9mwzANTqc/?ref=app
If you catch your exception specified like except ValueError: you no how you want to handle them. If it’s to general, like except: you don’t know what error are comming up, and it’s hard to know how to handle it. After the exception the code just run, and can affect your code in a way you don’t want. Then it’s better the error stop the code and you can read the error message and know how to handle it. If you test your code in a good way, you will be able to catch the most (common) error showing up.
Per Bratthammar, I catch the unexpected error because I run a server- it should run 24/7. If an error occurs, I need to close the servers socket and close the database properly.
What error is passing through?
Slick, that's the point - I don't know what to expect.. it's in case that an unexpected exception occurs...
It's always good to have a general except clause for the (in)famous "unexpected error".
Simon Sauter where should I mention it?
The general except statement has to be the last one so it is only executed when the error is not caught by one of the specific except clauses. And as Per mentioned it's generally a good idea to stop your program if the general clause is triggered (since you don't know what the error is you usually can't deal with it adequately). The only exception to that is during bug testing where you may want to keep the program running to see what else is going on. In that case you should still make sure that an error message is displayed. Otherwise you may be caught off guard.
Simon Sauter, yes, I understand. But I know that you should specify "checked" and "unchecked" exceptions in your code. I never understood where to type it and what each one means..
Per Bratthammar I don't mean you should *only* use a general except statement. But it's usually good to have it to catch those errors that you don't catch with the specific ones. Of course you should always think about what kind of errors might occur and how you want to take care of them. But there's always a chance there'll be an error you didn't think about. And for those it's good to have a general clause. So ideally you'd have something like this: If there's a type error do this. If there's a value error do this. If there's an index error do this. ... ... If there is any other error raise an exception, print "something unexpected went wrong" and stop the program.
You could do more than one, specific except statement. Or you could use the Finally keyword after except statement. Its a solution for "in every other case" beside the defined exception. It do not catch up a spezific errormsg for a problem in connection, serverside or in request-code. Maybe with a print statement for debugging reasons. Something like print("error in get_data func") I dont know the project, but maybe it helps to source out the errors in a own "assert" function. Instead to blow up the "catch up code" for specific errors in the main function which should do the job IF the stats are proved in step 1. Something like craft a single request for html status response, to check host or conection- specific values before initiate the socket and main programmcode.