Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python try except block does not recognize error type

Tags:

python

My scrpt has the following line:

libro_dia = xlrd.open_workbook(file_contents = libro_dia)

When libro_dia is not valid, it raises the following error:

XLRDError: Unsupported format, or corrupt file: Expected BOF record; found '<!DOCTYP'

I whant to handle this error, so I write:

try:
    libro_dia = xlrd.open_workbook(file_contents = libro_dia)
except XLRDError:
    no_termina = False

But it raises the following error:

NameError: name 'XLRDError' is not defined

What's going on?

like image 624
José Avatar asked Mar 01 '13 13:03

José


People also ask

How do you catch a type of error in Python?

Another way to catch all Python exceptions when it occurs during runtime is to use the raise keyword. It is a manual process wherein you can optionally pass values to the exception to clarify the reason why it was raised. if x <= 0: raise ValueError(“It is not a positive number!”)

How can the try except statements handle errors in Python?

The Python try… except statement runs the code under the “try” statement. If this code does not execute successfully, the program will stop at the line that caused the error and the “except” code will run. The try block allows you to test a block of code for errors.

When an error occurs within a try clause the code block will be executed?

It always executes, regardless of whether an exception was thrown or caught. You can nest one or more try statements. If an inner try statement does not have a catch -block, the enclosing try statement's catch -block is used instead. You can also use the try statement to handle JavaScript exceptions.

Can we use only try block in Python?

The try block lets you test a block of code for errors. The except block lets you handle the error. The else block lets you execute code when there is no error. The finally block lets you execute code, regardless of the result of the try- and except blocks.


2 Answers

You don't have XLRDError imported. I'm not familiar with xlrd, but something like:

from xlrd import XLRDError

might work. Alternatively, qualify your Error when handling it:

try:
    libro_dia = xlrd.open_workbook(file_contents = libro_dia)
except xlrd.XLRDError: #<-- Qualified error here
    no_termina = False

The above is assuming you have the following import:

import xlrd

In response to your comment:

There are several ways to use imports in python. If you import by using import xlrd, then you will have to qualify every object in that module as xlrd.SomeObject. An alternative way is by using the form from xlrd import *, which would allow you to reference the XLRD error without its' module namespace. This is lazy and a bad idea though, as it can lead to namespace clashes. If you would like to reference the error without qualifying it, the correct way to do it would be from xlrd import XLRDError, which would allow you to say except XLRDError. Read more about Python Modules

like image 64
mmoore Avatar answered Sep 19 '22 14:09

mmoore


XLRDError is a custom exception and must be imported to your namespace just like any other object.

Edit: As Burhan Khalid has noted, you could just modify the except block to except xlrd.XLRDError if you already have import xlrd in your module.

like image 29
Fabian Avatar answered Sep 19 '22 14:09

Fabian