Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MemoryError's message as str is empty in Python

This is a very silly question, but I'm running some tasks and catching their errors by:

try:
    run_something()
except Exception as e:
    handle_error(str(e))

I want the error message as a String because I'm using a UI and I want to display the error in a window.

The problem can be replicated as:

>>> import numpy as np
>>> np.range(1e10)
MemoryError                               Traceback (most recent call last)
<ipython-input-4-20a59c2069b2> in <module>() 
----> 1 np.arange(1e10)

MemoryError: 

However, if I try to catch the error and print its message (which I was hoping to be something like "MemoryError":

try:
    np.arange(1e10)
except Exception as e:
    print(str(e))
    print("Catched!")

The only output I get is "Catched!". This is so stupid, I was doing some work with UI's and threading, and took me a while to realise that the problem was a memory error with no message at all.

Is the MemoryError the only Exception that is translated to an empty string? Because if it is the case I can check it. If not, how to get its message as a String?

like image 539
Imanol Luengo Avatar asked Jul 05 '15 12:07

Imanol Luengo


People also ask

What is name error in Python?

What Is a NameError in Python? In Python, the NameError occurs when you try to use a variable, function, or module that doesn't exist or wasn't used in a valid way. Some of the common mistakes that cause this error are: Using a variable or function name that is yet to be defined.

What is the exception class in Python?

A custom exception class, MyCustomErrorInit is the method called when an instance is created and str is the magic method called when an instance is printed. Therefore, when an exception is raised, they are usually called in close succession. The raise statement in Python puts the programs into an error condition.


2 Answers

So you probably want to print the name of the exception class:

try:
    np.arange(1e10)
except Exception as e:   #not catch...
    print(str(e.__class__.__name__))
    print("Catched!")

Using str(e) only prints the "message" of the exception, which in your case was empty.


Note that you can obtain the arguments passed to the exception constructor via the args attribute:

In [4]: try:
   ...:     raise ValueError(1,2,3)
   ...: except ValueError as e:
   ...:     print('arguments:', e.args)
arguments: (1, 2, 3)
like image 177
Bakuriu Avatar answered Oct 20 '22 05:10

Bakuriu


When call str(e) it returns the message of the Exception. Take an example -

NameError: name 'a' is not defined
^          ^
name       message

The part before : is the name of the exception, whereas the part after it is the message (arguments).

In the case of MemoryError , as you can see in your example -

MemoryError:

does not have an error message , only the name of the exception , hence you get the empty string.

I am not sure if there are other Exceptions that do not have an exception, but I believe it would be very rare to find such exceptions. Maybe you can print both the exception's name as well as the message , if you really want to take care of the MemoryError (and maybe other such rare exceptions without messages) , something like -

print(type(e).__name__ , ':', str(e))
like image 36
Anand S Kumar Avatar answered Oct 20 '22 06:10

Anand S Kumar