Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

deleted 2 characters in body
Source Link
Nico Schlömer
  • 59.7k
  • 36
  • 216
  • 291

Maybe I missed the question, but why not:

class MyException(Exception):
 pass

To override something (or pass extra args), do this:

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

That way you could pass dict of error messages to the second param, and get to it later with e.errors.

In Python 2, you have to use this slightly more complex form of super():

super(ValidationError, self).__init__(message)

Maybe I missed the question, but why not:

class MyException(Exception):
 pass

To override something (or pass extra args), do this:

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

That way you could pass dict of error messages to the second param, and get to it later with e.errors.

In Python 2, you have to use this slightly more complex form of super():

super(ValidationError, self).__init__(message)

Maybe I missed the question, but why not:

class MyException(Exception):
 pass

To override something (or pass extra args), do this:

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

That way you could pass dict of error messages to the second param, and get to it later with e.errors.

In Python 2, you have to use this slightly more complex form of super():

super(ValidationError, self).__init__(message)
make python 3 the default case
Source Link
Nico Schlömer
  • 59.7k
  • 36
  • 216
  • 291

Maybe I missed the question, but why not:

class MyException(Exception):
 pass
 class MyException(Exception):
 pass

Edit: to To override something (or pass extra args), do this:

class ValidationError(Exception):
 def __init__(self, message, errors):
 
 # Call the base class constructor with the parameters it needs
 super(ValidationError, self).__init__(message)
 
 # Now for your custom code...
 self.errors = errors
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

That way you could pass dict of error messages to the second param, and get to it later with e.errors.


Python 3 Update: In Python 3+2, you canhave to use this slightly more compact usecomplex form of super():

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
super(ValidationError, self).__init__(message)

Maybe I missed the question, but why not:

class MyException(Exception):
 pass

Edit: to override something (or pass extra args), do this:

class ValidationError(Exception):
 def __init__(self, message, errors):
 
 # Call the base class constructor with the parameters it needs
 super(ValidationError, self).__init__(message)
 
 # Now for your custom code...
 self.errors = errors

That way you could pass dict of error messages to the second param, and get to it later with e.errors


Python 3 Update: In Python 3+, you can use this slightly more compact use of super():

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

Maybe I missed the question, but why not:

 class MyException(Exception):
 pass

To override something (or pass extra args), do this:

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

That way you could pass dict of error messages to the second param, and get to it later with e.errors.

In Python 2, you have to use this slightly more complex form of super():

super(ValidationError, self).__init__(message)
Bounty Awarded with 50 reputation awarded by Community Bot
Added Python 3 note
Source Link
gahooa
  • 138.5k
  • 14
  • 101
  • 104

Maybe I missed the question, but why not:

class MyException(Exception):
 pass

Edit: to override something (or pass extra args), do this:

class ValidationError(Exception):
 def __init__(self, message, errors):
 
 # Call the base class constructor with the parameters it needs
 super(ValidationError, self).__init__(message)
 
 # Now for your custom code...
 self.errors = errors

That way you could pass dict of error messages to the second param, and get to it later with e.errors


Python 3 Update: In Python 3+, you can use this slightly more compact use of super():

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

Maybe I missed the question, but why not:

class MyException(Exception):
 pass

Edit: to override something (or pass extra args), do this:

class ValidationError(Exception):
 def __init__(self, message, errors):
 
 # Call the base class constructor with the parameters it needs
 super(ValidationError, self).__init__(message)
 
 # Now for your custom code...
 self.errors = errors

That way you could pass dict of error messages to the second param, and get to it later with e.errors

Maybe I missed the question, but why not:

class MyException(Exception):
 pass

Edit: to override something (or pass extra args), do this:

class ValidationError(Exception):
 def __init__(self, message, errors):
 
 # Call the base class constructor with the parameters it needs
 super(ValidationError, self).__init__(message)
 
 # Now for your custom code...
 self.errors = errors

That way you could pass dict of error messages to the second param, and get to it later with e.errors


Python 3 Update: In Python 3+, you can use this slightly more compact use of super():

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
implement awatt's comment
Source Link
Chris Martin
  • 30.9k
  • 12
  • 83
  • 142
Loading
Used the right capitalization for Errors/errors. The author forgot to update this.
Source Link
Paolo
  • 21.4k
  • 21
  • 78
  • 124
Loading
Loading
edited body
Source Link
gahooa
  • 138.5k
  • 14
  • 101
  • 104
Loading
added 331 characters in body; added 147 characters in body
Source Link
gahooa
  • 138.5k
  • 14
  • 101
  • 104
Loading
Source Link
gahooa
  • 138.5k
  • 14
  • 101
  • 104
Loading
lang-py

AltStyle によって変換されたページ (->オリジナル) /