You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I saw the code raises many exception from class Exception. First, the only way to handle differently multiple Exceptions is to check their message, which is error-prone and difficult to maintain.
defprocess1():
raiseBaseException("Wrong user input for field X") # Noncompliantdefprocess2():
raiseBaseException("Wrong configuration") # Noncompliantdefprocess3(param):
ifnotisinstance(param, int):
raiseException("param should be an integer") # Noncompliantdefcaller():
try:
process1()
process2()
process3()
exceptBaseExceptionase:
ife.args[0] =="Wrong user input for field X":
# process errorpasselife.args[0] =="Wrong configuration":
# process errorpasselse:
# re-raise other exceptionsraise
Standard Recommendation :
raise a more specific Built-in exception when one matches. For example TypeError should be raised when the type of a parameter is not the one expected.
create a custom exception class deriving from Exception or one of its subclasses. A common practice for libraries is to have one custom root exception class from which every other custom exception class inherits. It enables other projects using this library to catch all errors coming from the library with a single "except" statement
The text was updated successfully, but these errors were encountered:
I saw the code raises many exception from class
Exception
. First, the only way to handle differently multiple Exceptions is to check their message, which is error-prone and difficult to maintain.Standard Recommendation :
The text was updated successfully, but these errors were encountered: