![]() ![]() Then we can simply add a new condition and use the raise keyword to raise an exception which is already handled. Print("Please enter valid integer value")Ĭonsider the above code where we have handled ZeroDivisionError, in this code we want to add a new validation for restricting user from inputting negative values. If you have a piece of code where along with exception handling you have put in place some conditional statements to validate input etc, then in case of the conditions failing we can either just print a message or simple raise an exception which can then get handled by the common exception handling mechanism. Taking a simple usage example: raise ZeroDivisionError the message is different because the representation of the exception (when printed) includes the exception class name. ![]() While the try and except block are for handling exceptions, the raise keyword on the contrary is to raise an exception.įollowing is the syntax: raise EXCEPTION_CLASS_NAME (editing the answer in response to your edit:) when you raise an exception: you're creating an instance of Exception which will be filtered out by future except ValueError: statements. Deleting List Elements & other Functions. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |