Reputation: 73
I am still relatively new to python, and I am trying to read/catch/handle errors when using the Quandl api.
For example, if I type in a dataset that does not exist, I would like to "read" the error returned from the call, but as a newbie to python, I am not sure how to read the errors. The API document Quandl API shows HTTP errors and Quandl errors that I would like to be able to handle.
Here is a simple code sample that fails, and I am trying to read/catch/handle the error.
import pandas as pd
import Quandl as Q
df = Q.get("CME/PLZ2016")
The Traceback gives lots of details about the error and then has this section at the end:
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\PyProj\Quandl\WorkingSampleQuandlDownload.py", line 7, in <module>
df = Q.get(exchsymbol, trim_start=strstartdate)
File "D:\Python33\lib\Quandl\Quandl.py", line 124, in get
raise DatasetNotFound(error)
Quandl.Quandl.DatasetNotFound: Dataset not found. Check Quandl code: CME/PLZ2016 for errors
If I use the try/exception, I get: Dataset not found. Check Quandl code: CME/PLZ2016 for errors
So, I would like to ask for your assistance in teaching me how to read/catch/handle the above error from Quandl. Hopefully I can learn how to find out the Quandl error and the HTTP status.
Thanks in advance.
Upvotes: 2
Views: 1291
Reputation: 451
You need to import the NotFoundError from Quandl:
import pandas as pd
import Quandl as Q
from quandl.errors.quandl_error import NotFoundError
try:
df = Q.get("CME/PLZ2016")
except NotFoundError:
print(NameError)
Upvotes: 3
Reputation: 1267
You need to import the error first:
import Quandl as Q
from Quandl.Quandl import DatasetNotFound
try:
df = Q.get("CME/PLZ2016")
except DatasetNotFound:
# put your response code here
Upvotes: 0