开发者

Python Exception handling when Importing

开发者 https://www.devze.com 2023-04-07 03:29 出处:网络
First fil开发者_C百科e: class E1Exception (Exception): def __init__(self,x): self.x=x def raiser (self,x):

First fil开发者_C百科e:

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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消