Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,985 questions

51,929 answers

573 users

How to catch multiple exceptions in Python

4 Answers

0 votes
try:
    arr = {}
    n = arr[3]
except (KeyError, AttributeError) as e:
    print("KeyError or AttributeError")
finally:
    print("finally")


'''
run:

KeyError or AttributeError
finally

'''

 



answered Sep 25, 2017 by avibootz
0 votes
try:
    arr = {}
    n = arr.non_existing_field
except (KeyError, AttributeError) as e:
    print("KeyError or AttributeError")
finally:
    print("finally")


'''
run:

KeyError or AttributeError
finally

'''

 



answered Sep 25, 2017 by avibootz
0 votes
try:
    arr = {}
    n = arr.non_existing_field
except KeyError as e:
    print("KeyError:", e)
except AttributeError as e:
    print("AttributeError:", e)
finally:
    print("finally")


'''
run:

AttributeError: 'dict' object has no attribute 'non_existing_field'
finally

'''

 



answered Sep 25, 2017 by avibootz
0 votes
try:
    arr = {}
    n = arr[3]
except KeyError as e:
    print("KeyError:", e)
except AttributeError as e:
    print("AttributeError:", e)
finally:
    print("finally")


'''
run:

KeyError: 3
finally

'''

 



answered Sep 25, 2017 by avibootz

Related questions

2 answers 162 views
1 answer 146 views
2 answers 271 views
1 answer 80 views
1 answer 199 views
1 answer 192 views
1 answer 192 views
...