Python How can I fix the error in my custom Python exception class?

  • Thread starter Thread starter tigger88
  • Start date Start date
  • Tags Tags
    Python
AI Thread Summary
The discussion revolves around creating a custom error class, MyError, that inherits from the built-in Exception class in Python. The purpose of this class is to handle incorrect inputs to the int() function and provide a meaningful error message that includes the input value. The user encountered an issue where the error output was "MyError: <unprintable MyError object>", which was suspected to be linked to the __init__ method. The provided code included attempts to define the error message but had commented-out sections and incorrect print statements in the __str__ method. The user also noted that the error message was printed twice. Ultimately, the user resolved the issue independently, indicating that the initial implementation was incorrect.
tigger88
Messages
17
Reaction score
0

Homework Statement


For an introductory Python course, one of the problems I am required to do involves creating a MyError class that inherits from Exception. Its purpose is to handle possible wrong inputs to the int() function and to return an error statement that includes what was input.

I have spent AGES on this (far too long) and finally have something that is semi-functional. Currently, however, I get an error saying "MyError: <unprintable MyError object>.

I think it's caused by the def __init__ method but I don't know how to fix it.
2. The attempt at a solution
Here is my code:
import exceptions
class MyError(Exception):
def __init__(self,value):
Exception.__init__(self,value)
self.value = value
#self.message = "You entered: " repr(self.value) ".\nPlease enter a previously defined variable, number or purely numeric (and non-decimal) string."
def __str__(self):
#return self.message
print "\nYou entered: ", repr(self.value), ".\nPlease enter a previously defined variable, number or purely numeric (and non-decimal) string."

try:
print int(thing)
except NameError as inst:
temp = str.split(list(inst.args)[0])
param = str(temp[1])
param = param.replace("'","")
raise MyError(param)
pass
except (TypeError,ValueError,SyntaxError):
stuff = repr(thing)
raise MyError(stuff)
pass

And this is the output in the interpreter (if it's relevant, I am using the python interpreter in Emacs):

>>>
You entered: 'thing'
Please enter a previously defined variable, number or purely numeric (and non-decimal) string.

You entered: 'thing'
Please enter a previously defined variable, number or purely numeric (and non-decimal) string.
Traceback (most recent call last):
File "/tmp/py14824uqc", line 9, in <module>
raise MyError(param)
MyError: <unprintable MyError object>

I don't know why it's printing the message twice either, but to be honest I can live with that.

Thanks for any help!
 
Last edited:
Technology news on Phys.org
Never mind - got it working. It was just completely wrong.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top