I`m trying to handle the exception 'ProfileNotExistsException' and the code is as follows:
try:
profile = instaloader.Profile.from_username(bot.context, followees[i])
#Other statements
except ProfileNotExistsException:
print('exception')
But This NameError is occurring.
NameError Traceback (most recent call last)
Input In [11], in <cell line: 2>()
6 allfolloweesDataList.append((profile.userid,profile.username,profile.full_name,profile.followers,profile.followees,profile.is_verified,profile.biography,profile.external_url))
7 print(i,end=',')
----> 8 except ProfileNotExistsException:
9 print('exception')
NameError: name 'ProfileNotExistsException' is not defined
How can I define a name for an exception like this?
CodePudding user response:
Looking at the source code for the instaloader
package, I believe this is what you are looking for:
from instaloader.exceptions import ProfileNotExistsException
try:
profile = instaloader.Profile.from_username(bot.context, followees[i])
...
except ProfileNotExistsException:
print('exception')
CodePudding user response:
Your question is not clear enough. There are three options.
I. You don't have such an existing class
class ProfileNotExistsException(ValueError):
def __init__(self):
pass
def __str__(self) -> str:
return 'Profile doesn\'t exist!'
You need to create such a class, and based on its name, it should extend ValueError
. Then you can catch it.
II. There is such a class
Just import it, or in your case it might be instaloader.ProfileNotExistsException
III. You actually don't want to catch only this exception
Use
try:
pass #...
except BaseException: # ValueError IndexError...
pass #...
CodePudding user response:
Thanks to all the contributors. The real problem was that the exception 'ProfileNotExistsException' was not defined in the code. We can handle the common exceptions easily as they are already defined check the list of a built-in exceptions.
So, If the exception is not the one from the built-in exceptions, we have to define this exception in our code before calling it. Thanks