Quantcast
Viewing latest article 2
Browse Latest Browse All 2

Catch specific errors in python

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 latest article 2
Browse Latest Browse All 2

Trending Articles