![]() How to customize the error message in our custom exception class? Now that we understand how to choose a class to inherit from, let us next have a look at how to customize the default error messages that these classes return. Return 'You did not select an option provided!' In the code below, we are collecting values from the user and to tell the user that there is an error in the value entered we are using the ValueError class. For the sake of keeping this example simple, I have chosen the higher-level exception type named “ Exception” class to inherit from. If you are looking for getting an appropriate Exception class to inherit from, I recommend having a look at all the built-in exceptions from the official python page here. In the above example, I have inherited from the Exception class in python, but the recommended practice is to choose a class that closely resembles your use-case.įor example, say you are trying to work with a string type object and you are given a list type object instead, here you should inherit your custom exception from TypeError since this Exception type closely resembles your use case which is “the variable is not of expected type”. Print(repr(e)) How to choose the exception class to inherit from? In Python, you can define your own custom exception classes by inheriting from another Exception class as shown in the code below. ![]() Option#2: Using Custom Exception classes to get customized error messages Now that we have learned how to print the default string which comes with an exception object, let us next learn how to customize the message that e carried (the string ‘ division by zero‘) and replace that with our own custom error message. There I have explained the 3 ways through which you can catch a list of exceptions along with tips on when is the right situation to catch each of these exceptions. Python: 3 Ways to Catch Multiple Exceptions in a single “except” clause If you are not exactly sure how to catch a list of exceptions using a single except clause, I suggest reading my other article in the link below. But this option of putting your custom messages into print statements might not work in cases where you might be handling a list of exceptions using a single except clause. This is the simplest way to print error messages in python. To summarize if you wish to print out the default error message along with a custom message use Option#1. This object is stored in the variable “ e” (line 4), which returns the string ‘ division by zero‘ when used with the print() statement (line 5). This line of code’s function is to catch all possible exceptions, whichever occurs first as an “ Exception” object. One thing to note here is the line “ except Exception as e“. ![]() Here the line “ x = 5/0″ in Example 1 above raised a “ ZeroDivisionError” which was caught by our except clause and the print() statement printed the default error message which is “ division by zero” to the standard output. A problem has occurred from the Problematic code: division by zero Running this code will give the output below. Print('A problem has occurred from the Problematic code: ', e) #Some Problematic code that can produce Exceptions The first and easiest option is to print error messages using a simple print() statement as shown in the example below. Option#1: Using a simple print() statement Let us start with the simplest of the 3, which is using a print() statement. There are 3 ways to print custom error messages in Python. So, let’s begin! Printing Custom Error messages It does not store any personal data.2 Choosing Parts of Default Error Messages to print The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. ![]() The cookie is used to store the user consent for the cookies in the category "Performance". This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other. The cookies is used to store the user consent for the cookies in the category "Necessary". The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional". The cookie is used to store the user consent for the cookies in the category "Analytics". These cookies ensure basic functionalities and security features of the website, anonymously. Necessary cookies are absolutely essential for the website to function properly.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |