Is There a way to catch each exception by its name using only one except | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Is There a way to catch each exception by its name using only one except

Say i Write following code: print("Hello World) print("a"+2) a = b+2 print(a) the above code has 3 error, is it possible to use only one except block? and it automatically detect the name of the other exception after solving previous one?

20th Jan 2017, 12:07 PM
Gaurav
Gaurav - avatar
4 Answers
+ 1
refer to exception type handler in my codes
20th Jan 2017, 6:48 PM
Sai Kiran Rayapureddy
Sai Kiran Rayapureddy - avatar
0
I say no because once it finds first exception it will come out of try block, so you can handle first exception only...
20th Jan 2017, 6:30 PM
Sai Kiran Rayapureddy
Sai Kiran Rayapureddy - avatar
0
Yes I know control will come out of try block, but I don't mean to say that it solve the exception automatically, I want to ask whether only one except block can tell the error name by its own if we don't know what might be the possible error Say in first line there is syntax error then type error then name error can we use something like this except Exception: print (Exception) and this always print the last line of the error you encounted each time
20th Jan 2017, 6:38 PM
Gaurav
Gaurav - avatar
0
once again thanks
21st Jan 2017, 3:00 AM
Gaurav
Gaurav - avatar