+ 1

how can I handle both or multiple exceptions with a single try and except blocks?

See this attached code below. I want to handle both exceptions ' ZeroDivisionError ' and ' TypeError ' at the same time. https://code.sololearn.com/c4nya8nJMp7j/?ref=app

22nd Jan 2020, 9:50 AM
Ashutosh R. Yadav
Ashutosh R. Yadav - avatar
8 Respostas
+ 7
Here is a solution that can catch all kind of exceptions, but it can not handle it individually. But i am sure it's worth to have a look on it. https://code.sololearn.com/cPgiDJLNwaQW/?ref=app
22nd Jan 2020, 10:27 AM
Lothar
Lothar - avatar
+ 4
乡Ashutosh°^°Kumar乡 I am not rude, I literally just told you that you can find the answer in google. If you can't even click on the first link of google, then how far do you think you'll go ? Using google (or any other engines) is a critical skill every programmer should know. People spend hours trying to find results (like me), and this is as easy as searching can be. It's literally the first link! https://i.imgur.com/HAXqOsA.jpg
22nd Jan 2020, 10:14 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 3
You can have multiple except statements one after another, if that's what you meant.
22nd Jan 2020, 9:52 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 2
乡Ashutosh°^°Kumar乡 Here is how: except (ZeroDivisionError, TypeError) as error: # Your code here I found the answer in the first link of google. Please search before posting next time. (edited)
22nd Jan 2020, 10:03 AM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 1
You can ask for several Exceptions at once like this. except (ZeroDivisionError, TypeError): But still only the first error will trigger the except block, and you can't get back to the try block after that. So if you need to take care of both problems separately, you have no choice but to use two separate try-except structures
22nd Jan 2020, 10:05 AM
HonFu
HonFu - avatar
+ 1
Lothar This is not executing anything after getting first error. Neither it showing the exception type. You can check it out here. https://code.sololearn.com/cpFuArOfu5N3/?ref=app
22nd Jan 2020, 11:05 AM
Ashutosh R. Yadav
Ashutosh R. Yadav - avatar
0
Aymane Boukrouh [INACTIVE] I want to use only one try and except block. As I have written the code, it contains two exceptions in try block. I want to handle both of them at the same time. That is what I need.
22nd Jan 2020, 10:00 AM
Ashutosh R. Yadav
Ashutosh R. Yadav - avatar
0
Damn man. How rude you are. Are you a member of regulatory committee of Solo Learn? Do anyone bothering you to answer the questions? Do follow the rules you want to follow. But don't try to discourage anyone by posting rules and regulations on every posts. Aymane Boukrouh [INACTIVE]
22nd Jan 2020, 10:10 AM
Ashutosh R. Yadav
Ashutosh R. Yadav - avatar