The python exception handling mechanism is similar to Java, using the try-except-finally structure.
Try-except Detecting anomalies
Format
try: try_statementexcept (ErrorType1, ErrorType2),e: handle_statementfinally: finally_statement
Instance
#!/usr/bin/pythontry: a=12 b=0 c = a/bexcept Exception, e: print "Exception occurs: " , efinally: print"finally handle!"
Context Manager (With...as ... Statement
The WITH statement can be especially useful for scenarios where the resource is finally freed up, because it automatically frees the owning resource and does not need to be displayed to release the resource
Format
with context_expr [asvar]: with_statement
Raise throws an exception
Format
Raise exception[, args] or raise Exception (args)
Instance
Raise Exception (' exampleexception ')
Assertion
Detecting key points of the program, triggering Asserterror (assertion error) when the assertion is unsuccessful
The format is as follows
Assert expression[, arguements]
Python Exception handling