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

  • Thread starter tigger88
  • Start date
  • Tags
    Python
In summary, the conversation is about a student struggling with creating a MyError class in Python and encountering errors while trying to handle wrong inputs to the int() function. The student shares their code and the output in the interpreter, and eventually figures out the problem.
  • #1
tigger88
21
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
  • #2
Never mind - got it working. It was just completely wrong.
 

1. What are custom exceptions in Python?

Custom exceptions in Python are user-defined exceptions that allow you to create your own error classes. This can be useful when you want to handle specific errors in a certain way, rather than using the built-in exceptions provided by Python.

2. How do you create a custom exception in Python?

You can create a custom exception in Python by defining a new class that inherits from the built-in Exception class. This new class can then be raised like any other exception using the raise keyword.

3. What is the purpose of using custom exceptions in Python?

The purpose of using custom exceptions in Python is to handle specific errors in a more targeted and meaningful way. This can make your code more readable and maintainable, as well as providing better error messages for debugging.

4. Can custom exceptions be raised for any type of error?

Yes, custom exceptions can be raised for any type of error. They can be used to handle errors related to data validation, input/output, or any other specific situations that may occur in your code.

5. How do you handle custom exceptions in Python?

Custom exceptions can be handled using a try/except block, just like any other exception. You can also specify multiple except clauses to handle different types of custom exceptions separately, or use the finally clause to execute code regardless of whether an exception is raised.

Similar threads

  • Programming and Computer Science
Replies
9
Views
909
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
13
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
5
Views
9K
  • Programming and Computer Science
2
Replies
43
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
Back
Top