即使我抓住了它,但抛出了例外
我正在尝试列举收集URL,以使用。 当轨道ID不存在时,即使我正在捕获所述的确切错误,该程序也会引发错误。该错误说在处理异常时,发生了另一个例外,这是否意味着我设置了我的尝试不正确?
import spotipy
from spotipy.oauth2 import SpotifyOAuth
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id="...",
client_secret="...",
redirect_uri="http://localhost/",
scope="playlist-modify-public",
requests_timeout=5))
try:
track = sp.track('7IaxPh0ttHTsJ4rVPPiQ9V', 'NL')
except sp.SpotifyException:
x = 0
这是我收到的错误消息:
Exception has occurred: SpotifyException
http status: 404, code:-1 - https://api.spotify.com/v1/tracks/7IaxPh0ttHTsJ4rVPPiQ9V?market=NL:
Non existing id: 'spotify:track:7IaxPh0ttHTsJ4rVPPiQ9V', reason: None
During handling of the above exception, another exception occurred:
File "C:\Users\Dirkv\OneDrive\Documents\Projects\SelfStudy\Python\Music Bot\music_bot.py", line 38, in <module>
track = sp.track(url, 'NL')
I'm trying to enumerate a collection URLs to get corresponding spotify tracks using Spotipy.
When a track ID does not exist, the program throws an error, even though I'm catching the exact error mentioned. The error says that during handling of the exception another exception occured, does this mean I've setup my try catch incorrectly?
import spotipy
from spotipy.oauth2 import SpotifyOAuth
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id="...",
client_secret="...",
redirect_uri="http://localhost/",
scope="playlist-modify-public",
requests_timeout=5))
try:
track = sp.track('7IaxPh0ttHTsJ4rVPPiQ9V', 'NL')
except sp.SpotifyException:
x = 0
This is the error message I get:
Exception has occurred: SpotifyException
http status: 404, code:-1 - https://api.spotify.com/v1/tracks/7IaxPh0ttHTsJ4rVPPiQ9V?market=NL:
Non existing id: 'spotify:track:7IaxPh0ttHTsJ4rVPPiQ9V', reason: None
During handling of the above exception, another exception occurred:
File "C:\Users\Dirkv\OneDrive\Documents\Projects\SelfStudy\Python\Music Bot\music_bot.py", line 38, in <module>
track = sp.track(url, 'NL')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要从 spotipy 导入错误,而不是 sp
注意:
请勿发布您的 client_id 或 client_secret 为了您自己的安全
You need to import errors from spotipy not from sp
NOTE :
DO NOT post your client_id or client_secret for your own safety