Exception handling:
Grammar:
Try
Code
Except (keyerror. Can write Multiple) as e:error to fetch multiple error messages, e is error message
Print (e) # printing error message
Except (indexerror) as E:
Print (e)
Except Exception as E: # errors that cannot be captured by the above method are captured here
Print (' unknown error ', E)
Else: # when no error occurs, execute here
Print (' everything OK ')
Finally
Print (' It will be done here with or without errors ') '
Python Standard exception
Exception Name Description
Baseexception base class for all exceptions
Systemexit Interpreter Request Exit
Keyboardinterrupt user Interrupt execution (usually input ^c)-------"common
Exception base class for general errors
Stopiteration iterator with no more values
Generatorexit Generator (generator) exception occurred to notify exit
StandardError base class for all built-in standard exceptions
Arithmeticerror base class for all numeric calculation errors
Floatingpointerror Floating-point calculation error
Overflowerror numeric operation exceeds maximum limit
Zerodivisionerror except (or modulo) 0 (all data types)
Assertionerror Assertion Statement failed
Attributeerror object does not have this property--------"commonly used
Eoferror has no built-in input to reach the EOF tag
EnvironmentError base class for operating system errors
IOError Input/output Operation Failed--------"common
OSError Operating system error
Windowserror system call failed
Importerror Import Module/object Failed--------"common
Lookuperror base class for invalid data queries
This index (index)-----is not used in the indexerror sequence
Keyerror map does not have this key--------"commonly used
Memoryerror Memory overflow error (not fatal for Python interpreter)
Nameerror Object Not declared/initialized (no Attributes)------------"common
Unboundlocalerror access to uninitialized local Variables--------"common
Referenceerror Weak reference (Weak Reference) attempts to access objects that have been garbage collected
RuntimeError General Run-time Errors
Notimplementederror methods that have not yet been implemented
SyntaxError Python Syntax Error-------------"common
Indentationerror Indentation Error----------"commonly used
Taberror Tab and Space mix
Systemerror General Interpreter System error
TypeError Operations-------that are not valid for types are commonly used
ValueError passed invalid parameter---------"commonly used
Unicodeerror Unicode-related Errors
Unicodedecodeerror Unicode decoding Error
Unicodeencodeerror Unicode Encoding Error
Unicodetranslateerror Unicode Conversion Error
Base class for Warning warnings
Deprecationwarning warning about deprecated features
Futurewarning warning about the change in the construction of future semantics
overflowwarning old warning about auto-promotion to long integer
Pendingdeprecationwarning warnings about attributes that will be discarded
Runtimewarning warning for suspicious runtime behavior (runtime Behavior)
Syntaxwarning warning of suspicious syntax
Userwarning warning for user code generation
Example:
# Custom Exceptions
1 classzyexception (Exception):2 def __init__(self,msg):3Self.msg =msg4 #the following line, if present, returns an error based on the following line, and returns the SELF.MSG information if it does not exist5 def __str__(self):6 #return self.msg7 return 'Custom Error'8 Try:9 RaiseZyexception ('database cannot connect')Ten exceptzyexception as E: one Print(e)View Code
Python--exceptions