Quantcast
Channel: Catch specific errors in python - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Catch specific errors in python

$
0
0

I was trying to catch a specific exception:

username = 'myuser'try:    user = User.objects.get(username=username)    print(user)except Exception as e:    if type(e)=='django.contrib.auth.models.User.DoesNotExist':        print('No such user')    print (type(e))

But instead of going into the if loop, I am getting:

<class 'django.contrib.auth.models.User.DoesNotExist'>

Why is this happening? How can I catch a specific exception?


Viewing all articles
Browse latest Browse all 2

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>