0
class client():
    def __init__(self,identitate,nume,cnp,filme_inchiriate,inchirieri):
        self.__identitate=identitate
        self.__nume=nume
        self.__cnp=cnp
        self.__filme_inchiriate=filme_inchiriate
        self.__inchirieri=inchirieri  

    def get_identitate(self):
        return self.__identitate


    def get_nume(self):
        return self.__nume


    def get_cnp(self):
        return self.__cnp

    def get_filme_inchiriate(self):
        return self.__filme_inchiriate


    def get_inchirieri(self):
        return self.__inchirieri


    def set_identitate(self, value):
        self.__identitate = value


    def set_nume(self, value):
        self.__nume = value


    def set_cnp(self, value):
        self.__cnp = value


    def set_filme_inchiriate(self, value):
        self.__filme_inchiriate = value


    def set_inchirieri(self, value):
        self.__inchirieri = value


    def del_identitate(self):
        del self.__identitate


    def del_nume(self):
        del self.__nume


    def del_cnp(self):
        del self.__cnp


    def del_filme_inchiriate(self):
        del self.__filme_inchiriate


    def del_inchirieri(self):
        del self.__inchirieri

    identitate = property(get_identitate, set_identitate, del_identitate, "identitate's docstring")
    nume = property(get_nume, set_nume, del_nume, "nume's docstring")
    cnp = property(get_cnp, set_cnp, del_cnp, "cnp's docstring")
    filme_inchiriate = property(get_filme_inchiriate, set_filme_inchiriate, del_filme_inchiriate, "filme_inchiriate's docstring")
    inchirieri = property(get_inchirieri, set_inchirieri, del_inchirieri, "inchirieri's docstring")
    def __str__(self):
        return "ID: " + str(self.get_identitate()) + " Nume: " + str(self.get_nume()) + " CNP: "+ str(self.get_cnp())


from entities import *

class validator_client():

     def validate_client(self,client):
         erori=[]
         if client.get_identitate=="":
             erori.append("Nu ati introdus ID!")
         if client.get_nume=="":
             erori.append("Nu ati indorus nume!")
         if len(erori)>0:
             raise ValidatorException(erori)

     def haha(self,client):
         if client.get_identitate()=="1":
             print "hahahah" 

class ValidatorException(Exception):
     def __init__(self,erori):
         self.__erori=erori
     def get_erori(self):
         return self.__erori
     def __str__(self):
         return self.erori
     erori = property(get_erori, None, None, None)


client1=client("",2,3,4,5)
validare=validator_client()
try:
     validare.validate_client(client1)
except:
     ValidatorException
     print (ValidatorException)

client()是一个有 5 个属性的类,其中第一个是 id,它有一个 getter 和 setter,所以这个类没有问题,但是为什么在我运行它时没有打印任何错误?

4

1 回答 1

5

一些事情:你没有调用那个getter,你正在获取方法(除非它是一个属性,在这种情况下,为什么它被称为get)。此外,正如@Martijn Pieters 在评论中所说,您的 except 子句捕获所有异常,然后打印ValidatorException类的字符串表示,而不是异常实例。

except条款而言,我认为您可能正在寻找的是:

except ValidatorException as ve:
    print(ve)
于 2012-11-16T14:09:20.990 回答