On 02/17/2014 06:44 AM, Khalid Al-Ghamdi wrote:
Hi,

Why is it i can use mu custom class exception without creating an exception
object first?

Thanks


    1. class ShortInputException(Exception): def __init__(self, length,
     atleast):
    2.         Exception.__init__(self)
    3.         self.length = length
    4.         self.atleast = atleast
    5. try:
    6. text = input() if len(text) < 3:
    7. raise ShortInputException(len(text), 3) # Other work can continue as
    usual here
    8. except EOFError:
    9. print()
    10. except ShortInputException as ex:
    11. print(\
    12. .format(ex.length, ex.atleast)) else:
    13. print()



Your code posted here is totally broken. indentation is missing and lines are illegally combined. And somehow you added colors and incorrect line numbers besides.

Please post here in plain text, not html, and without line numbers, colorizing or other nonsense.

Now to your question. I don't know what you mean "without creating an exception object". You create one in the raise statement. It is dubious however to raise an exception in any __init__ routine, since that could perhaps mean that not all the intiialization has actually happened. And it could lead to infinite recursion, if the exception is the same class as you're initializing. I would also strenuously avoid doing input() or other blocking operations in the __init__ routine, but I'm not sure I know of any real reason why.

if this is real code, and not just an experiment, could you explain (after formatting it in a text message so we can actually read it) just what does happen, and what you expected/wished to happen?

(You might not see this message or my others for quite some time, as it seems I've been dropped from the approved posters by having joined the mailing list ???)

--
DaveA

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to