0

First file:

class E1Exception (Exception):

    def __init__(self,x):
        self.x=x


    def raiser (self,x):
        self.x=x
        if x=='So sue me':
            raise E1Exception('New Yorker')
        else:
            try:
                number = (int)(x)
                pass
            except ValueError:
                raise ValueError ()

Second file:

import e1a
from e1a import *


def reporter (f,x):

    try:
        print f(x)
        return ('no problem')
    except ValueError:
        return ('Value')
    except E1Exception:
        return ('E1')
    else:
        return ('generic')

Question 1:

Does the function raiser have to be static in order to be used in the second file?

The problem is the E1Exception is never caught any solution?

2
  • I edited your answer to use the code formatter. You should highlight any code that you copy/paste and click the bracket button at the top enable the syntax highlighting. Commented Sep 25, 2011 at 18:00
  • Using 'else' is probably not what you want to do. Statements in the else block are executed if no error was raised. Commented Sep 25, 2011 at 18:54

2 Answers 2

1

The problem is that the error is never "raised"

http://docs.python.org/tutorial/errors.html

You have to write raise E1Exception(x) somewhere with some x value.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Falmarri for mentioning the error. The first code works fine, but the second except E1Exception: return ('E1') The E1Exception error is never raised, eventhough i imported the module. Question: The function reporter takes in a function and a variable x. can I use a fuction from the E1a module. how would it look like? reporter (E1Exception.raiser, 'So sue me') ??
0

Does the function raiser have to be static in order to be used in the second file?

Python doesn't have a concept of "static". What do you mean by "static"?

Also, I don't think you realize what (int)(x) is doing. That looks to me like you're trying to cast x as an int. And although it works, it's only by coincidence. What you're really doing is invoking the int function on x. So that is equivalent to

number = int(x)

That's not related to your questions, but I thought I should point that out in case anyone gets confused.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.