Python basics why exception handling doesn't work?

I am new to python development and trying to understand exception handling. I have 2 functions of the 1st to find the 1st occurrence of a duplicate value in the list and the 2nd to call the function. But if you try to pass the name of a list that does not exist, it should print a message with the exception of the block, but it does not work, please help

def first_duplicate(a): x = 0 for i, j in enumerate(a): for k in a[i + 1:]: if j == k: x = 1 return j if x == 1: break def call_main (list_name): try: x = first_duplicate(list_name) if x is None: print("No duplicates") else: print(x, "is the first duplicate") except NameError: print("exception occurred Name ") except ValueError: print("exception occurred value") 

my team is like

 call_main(y) 

where I did not create a list: y, it should print an exception message in this case, but it is not there, please help

+5
source share
2 answers

Here is an example. In the code below, try has a name y that is not an input, so it throws a NameError exception.

t=[11]; function(t); raises a NameError .

t=(1,2); function(t) t=(1,2); function(t) will raise an AttributeError .. because x.append(5) hooked first.

 def function(x): try : x.append(5); x+y except NameError: print("name error exception") except AttributeError: print("attribute error excetption") t=[11]; function(t) t=(1,2) function(t) 

Now, if you call function(a_list) , which a_list is not yet defined, it will raise the default NameError . Because the error occurs even before the code inside the function is processed. Thus, try is not even processed. I think this should explain why you get a default NameError .


If you want to create your NameError , you can also (in addition) set try outside the scope of functions.

 try : call_main(y); except NameError: print("something"); 
+2
source

Basically, in any code you write, try: except blocks can catch errors only for lines passing inside them. If you cause an error in the function call (or code that actually wraps the try: except block), the error is executed before testing, and therefore the error cannot be detected there.

That is why in order to catch the error caused by the argument before it is passed to the function (or the syntax of the function call itself), as in call_main(y) , the error handling must be moved beyond this as shown by others, here with code:

 try: call_main(y) except NameError: print("exception caught a NameError") 

Exception handling can only do so much. At some point, the programmer must confirm his code and make sure that it works. In an appropriate note, it is best to include a general exception try: except in your try: except blocks in order to catch any unexpected errors that you have not tested or thought of yet. The following modification of your code demonstrates this, and also shows how to get the computer to tell you what error it caught:

 def call_main (list_name): try: x = first_duplicate(list_name) if x is None: print("No duplicates") else: print(x, "is the first duplicate") except NameError: print("exception occurred Name ") except ValueError: print("exception occurred value") except Exception as ee: print(ee) print(type(ee)) 

In the last note, to prove that your exception handling works, make the following changes to x = first_duplicate(list_name) and run it.

x = first_duplicate(y)

Result: the code catches a name error

x = first_duplicate(0)

Result: a general exception is found and the type of error is identified for you

+2
source

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


All Articles