+ 1
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: ....
13 Respostas
0
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.
+ 2
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.
+ 1
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
+ 1
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.
+ 1
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.
0
What error is passing through?
0
Slick, that's the point - I don't know what to expect.. it's in case that an unexpected exception occurs...
0
It's always good to have a general except clause for the (in)famous "unexpected error".
0
Simon Sauter where should I mention it?
0
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.
0
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..
0
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.
0
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.