djc1223
djc1223

Reputation: 55

Handle exceptions fo empty or null input?

I'm working on a class project to implement a basic voting system using blockchain, but am not super familiar with exception handling in python. I have a constructor for my ballot class which initiates two variables and an object: id, candidate and person. How would I throw an exception if any of those variables/object are empty or have null input?

 def __init__(self, person, id: int, candidate: str):
        """create and store the ballot instance using voter object and id"""
        self.vote = {"ID": id, "SIG": person.hash, "CANDIDATE": candidate}
        self._store_ballot(person.name)

Upvotes: 0

Views: 147

Answers (1)

Jérôme Fink
Jérôme Fink

Reputation: 176

You would have to test your value at some point and use the keyword raise to throw the wanted exception. You could use one of the built-in exceptions or create a custom one depending on your needs. Read more about that here : https://docs.python.org/3/library/exceptions.html

Upvotes: 1

Related Questions