in Education by
What's the proper way to declare custom exception classes in modern Python? My primary goal is to follow whatever standard other exception classes have, so that (for instance) any extra string I include in the exception is printed out by whatever tool caught the exception. By "modern Python" I mean something that will run in Python 2.5 but be 'correct' for the Python 2.6 and Python 3.* way of doing things. And by "custom" I mean an Exception object that can include extra data about the cause of the error: a string, maybe also some other arbitrary object relevant to the exception. I was tripped up by the following deprecation warning in Python 2.6.2: >>> class MyError(Exception): ... def __init__(self, message): ... self.message = message ... >>> MyError("foo") _sandbox.py:3: DeprecationWarning: BaseException.message has been deprecated as of Python 2.6 It seems crazy that BaseException has a special meaning for attributes named message. I gather from PEP-352 that attribute did have a special meaning in 2.5 they're trying to deprecate away, so I guess that name (and that one alone) is now forbidden? Ugh. I'm also fuzzily aware that Exception has some magic parameter args, but I've never known how to use it. Nor am I sure it's the right way to do things going forward; a lot of the discussion I found online suggested they were trying to do away with args in Python 3. Update: two answers have suggested overriding __init__, and __str__/__unicode__/__repr__. That seems like a lot of typing, is it necessary? Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
There are many ways to declare the custom exception in Python 2 version:- One way you can use super() method to declare the custom exception : class ValidationError(Exception): def __init__(self, message, errors): # Call the base class constructor with the parameters it needs super().__init__(message) # Now for your custom code... self.errors = errors Exceptions in modern Python such as Python 3+, don't need to abuse .message, or override .__str__() or .__repr__() or any of it. If all you want is an informative message when your exception is raised, do this: class MyException(Exception): Pass raise MyException("ABC")

Related questions

0 votes
    When you just want to do a try-except without handling the exception, how do you do it in Python? Is the ... rmtree(path) except: pass Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    I want to raise an exception in Python and in future want to detect it using an except block, Can someone tell me how to do it? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    Which of the following classes can catch all exceptions which cannot be caught? (a) RuntimeException (b) Error (c ... Handling of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    Which of these class is related to all the exceptions that can be caught by using catch? (a) Error (b) ... Exception Handling of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    Which of these class is related to all the exceptions that cannot be caught? (a) Error (b) Exception (c ... Exception Handling of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    Which of these class is related to all the exceptions that are explicitly thrown? (a) Error (b) Exception ... Handling of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    How can I write a try/except block that catches all exceptions? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    Which of these keywords must be used to monitor for exceptions? (a) try (b) finally (c) throw (d) ... Exception Handling of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    Which of these keywords are used for the block to be examined for exceptions? (a) try (b) catch (c) ... Exception Handling of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    Which of these keywords are used for the block to handle the exceptions generated by try block? (a) try ... Exception Handling of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    I am working on a C++ program, which uses cURL library, which is written in plain C. When I try ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Aug 1, 2022 in Education by JackTerrance
0 votes
    When does Exceptions in Java arises in code sequence? (a) Run Time (b) Compilation Time (c) Can Occur ... Exception Handling of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    Which of these clause will be executed even if no exceptions are found? (a) throws (b) finally (c) ... Exception Handling of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    Which of these exceptions handles the divide by zero error? (a) ArithmeticException (b) MathException (c) ... Handling of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    Which of these classes is used to define exceptions? (a) Exception (b) Throwable (c) Abstract (d) System ... Exception Handling of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
...