This section describes the principles and forms of exception handling in Python. This section describes the principles and forms of exception handling in Python.
1. what is an exception?
In Python, exception objects are used to indicate exceptions. An exception occurs when the program encounters an error during running. If the exception object is not processed or captured, the program will trace back and terminate the execution.
2. throw an exception
The raise statement is followed by the Exception class or the Exception subclass. you can also add the Exception information to the brackets of the Exception.
>>> Raise Exception ('message ')
Note: The Exception class is the base class of all Exception classes. we can also create our own defined Exception classes based on this class, as shown below:
Class SomeCustomException (Exception): pass
3. catch exceptions (try/retry T statement)
The try/try T statement is used to detect errors in the try statement block, so that the try T statement can capture and handle exceptions.
Multiple exceptions can be thrown in a try statement block:
try: x = input('Enter the first number: ') y = input('Enter the second number: ') print x/y except ZeroDivisionError: print "The second number can't be zero!" except TypeError: print "That wasn't a number, was it?"
One T t statement can capture multiple exceptions:
Try: x = input ('Enter the first number: ') y = input ('Enter the second number:') print x/y prior t (ZeroDivisionError, TypeError, NameError ): # note that the parentheses after the limit T statement print 'your numbers were bogus...'
Access the caught exception object and print the exception information:
try: x = input('Enter the first number: ') y = input('Enter the second number: ') print x/y except (ZeroDivisionError, TypeError), e: print e
Capture all exceptions to prevent unexpected exceptions from being missed:
try: x = input('Enter the first number: ') y = input('Enter the second number: ') print x/y except : print 'Someting wrong happened...'
4. else clause. Besides using the distinct T clause, you can also use the else clause. If no exception is thrown in the try block, the else clause is executed.
while 1: try: x = input('Enter the first number: ') y = input('Enter the second number: ') value = x/y print 'x/y is', value except: print 'Invalid input. Please try again.' else: break
After the preceding code block is run, if the x and y values entered by the user are valid, the else clause is executed to exit the program.
5. finally clause. Whether exceptions occur in the try clause, the finally clause is certainly executed and can be used with the else clause. Finally clauses are usually used to close files or network sockets at the end of a program.
try: 1/0 except: print 'Unknow variable' else: print 'That went well' finally: print 'Cleaning up'
6. exceptions and functions
If an exception is thrown in the function but not processed, it will be passed to the function call. if The exception is not processed, it will be passed to the main program, terminate a stack trace.
def faulty(): raise Exception('Someting is wrong!')def ignore_exception(): faulty() def handle_exception(): try: faulty() except Exception, e: print 'Exception handled!',e handle_exception()ignore_exception()
In the preceding code block, after the function handle_exception () calls faulty (), the faulty () function throws an exception and is passed to handle_exception (), which is processed by the try/try T statement. The ignare_exception () function does not handle the faulty () exception, causing stack tracing exceptions.
Note: if/esle can implement the same functions as exception handling, but the conditional statements may be less natural and readable.