basic exception handling python

I. Introduction abnormal

1. Abnormal

In the process of running the program, it is inevitable there will be some errors, such as:

Use the variable assignment had not

Use the index does not exist

In addition to 0

...

These errors in the program, we call abnormal.

The program is running, once the exception will cause the program to terminate immediately, after all exception code will not be executed!

name Error Variable not defined

Common types of exceptions Chinese Description:

Systax Error Syntax Error

IO Error File Error

Zero DivisionError 10/0, divide by zero error

Value Error error value

Keyboard Interrupt forced termination program generates, ctrl + c terminate the program

 

2. Handling Exceptions

An exception occurred when the program is running, our purpose is not to make direct program termination!

Python is hope when an exception occurs, we can write code to handle the abnormal!

try statement

try:

Code block (errors that may appear statement)

except as abnormal exception type name:

Code block (subsequent error handling)

except as abnormal exception type name:

Code block (subsequent error handling)

except as abnormal exception type name:

Code block (subsequent error handling)

else:

Code block (error statement is not to be performed)

finally:

Code block (the block of code will always execute)

is a must try statement has no else will do

except that at least one and finally

Possible error codes can be placed to try statement, so if the code is not an error, it will perform properly,

If an error occurs, the code will be executed expect clause, so that we can through the code to handle exceptions

Avoid because of an unusual result in the termination of the entire program

Second, the exception propagation

Abnormal propagation (thrown)

When an exception occurs in the function, if an exception were processed in the function, the exception will not continue to spread,

If there is no abnormality handler, the exception will continue to spread to the calling of the function,

If the function of the handle an exception, not spread if not treated will continue to spread to the call at

Until transferred to the global scope (master module) If still not processed, the program terminates and displays the abnormality information

When an exception occurs during program execution, all exception information is saved in a special exception object,

And it propagates, in fact, call the exception object thrown at

For example: an object to represent a special class ZeroDivisionError exception other 0

Object classes designed to NameError error exception process variable

....

Python provides a number of anomalies in the target for us

Throw an exception

- You can raise statement to throw an exception,

- the need to raise statement with an example of an exception class or abnormal

Common types of exceptions Chinese Description:

Third, the exception object

print ( 'before abnormal')

l = []

try:

# print(c)

# l[10]

print(10/0)

except NameError:

# If except not with any of the content, at which point it will capture all exceptions

# If followed an unusual type after except, so this time it will only capture the type of exception

print ( 'abnormality occurs NameError')

except ZeroDivisionError:

print ( 'abnormality occurs ZeroDivisionError')

except IndexError:

print ( 'abnormality occurs IndexError')

# Exception is the superclass of all exception classes, except if it is followed by Exception, he would capture all exceptions

# At this point you can follow a as xx xx is the exception class exception object in the back

except Exception as e :

print ( 'unknown exception', e, type (e))

finally :

print ( 'regardless of whether an exception occurs, the clause will be executed')

print ( 'the abnormality')

Fourth, the custom exception objects

Exception object handling system using built-in exceptions, you can also create your own exception classes

Custom exception class, just create a class that inherits Exception (all exception classes parent class) to

class MyError (Exception): # create a subclass of Exception

pass # we can write the main part of the properties and methods can also be left blank

to raise an exception is thrown to the outside (overall), with a possible exception behind class or instance exception class,

def add(a,b):

# If there are negative numbers a and b, throwing an exception to the call at

if a < 0 or b < 0:

# raise Exception

There was a problem calling here # thrown objects, tell the caller you want to handle it yourself

# Raise Exception ( 'two parameters can not have negative!')

raise MyError ( 'custom exception')

# Handle may be replaced by the abnormality if else

# return None

return r

print(add(-123,456))

Custom function throws an exception, the situation can be resolved non-standard input or wrong, to force the caller to be modified, you can use an if statement and return in place, but relatively trouble

 

Guess you like

Origin www.cnblogs.com/sharan-coco/p/12641839.html