Error Handling Python Example
Contents |
you have probably seen some. There are (at least) two distinguishable kinds of errors: syntax errors and exceptions. 8.1. Syntax Errors¶ Syntax errors, also known as parsing errors, are perhaps the most common kind of complaint you
Python Try Statement
get while you are still learning Python: >>> while True print 'Hello world' File "
Python Try Finally
print, since a colon (':') is missing before it. File name and line number are printed so you know where to look in case the input came from a script. 8.2. Exceptions¶ Even if a statement or expression is syntactically correct, it may cause an error when an attempt is made to execute it. Errors detected during execution are called exceptions and are not unconditionally fatal: you will soon learn how to
Error Handling Python 3
handle them in Python programs. Most exceptions are not handled by programs, however, and result in error messages as shown here: >>> 10 * (1/0) Traceback (most recent call last): File "
Pages Local Site Map ------------------------ Rename Page Delete Page ------------------------ ------------------------ Remove Spam Revert to this revision ------------------------ SlideShow User Login Handling Exceptions The simplest way to handle exceptions is with a "try-except" block: 1 (x,y) = (5,0) 2 try: 3 z = x/y 4 except ZeroDivisionError: 5 print "divide by zero" If you wanted to examine the exception error handling in python script from code, you could have: 1 (x,y) = (5,0) 2 try: 3 z = x/y
Python Error Handling Best Practices
4 except ZeroDivisionError as e: 5 z = e # representation: " Error: %s
how to handle those. Exception handling allows us to continue our program (or terminate it) if an exception occurs. Error Handling Error handling in Python is done through the use of exceptions that http://www.pythonforbeginners.com/error-handling/python-try-and-except are caught in try blocks and handled in except blocks. Try and Except If an error is encountered, a try block code execution is stopped and transferred down to the except block. In addition to using an except block after the try block, you can also use the finally block. The code in the finally block will be executed regardless of whether an exception occurs. Raising an Exception You can raise an exception error handling in your own program by using the raise exception [, value] statement. Raising an exception breaks current code execution and returns the exception back until it is handled. Example A try block look like below try: print "Hello World" except: print "This is an error message!" Exception Errors Some of the common exception errors are: IOError If the file cannot be opened. ImportError If python cannot find the module ValueError Raised when a built-in error handling python operation or function receives an argument that has the right type but an inappropriate value KeyboardInterrupt Raised when the user hits the interrupt key (normally Control-C or Delete) EOFError Raised when one of the built-in functions (input() or raw_input()) hits an end-of-file condition (EOF) without reading any data Example Let's have a look at some examples using exceptions. except IOError: print('An error occured trying to read the file.') except ValueError: print('Non-numeric data found in the file.') except ImportError: print "NO module found" except EOFError: print('Why did you do an EOF on me?') except KeyboardInterrupt: print('You cancelled the operation.') except: print('An error occured.') There are a number of built-in exceptions in Python. Recommended Python Training – Treehouse For Python training, our top recommendation is Treehouse. Treehouse is an online training service that teaches web design, web development and app development with videos, quizzes and interactive coding exercises. Treehouse has beginner to advanced Python training that programmers of all levels benefit from. Tweet Read more about: Error Handling pythonforbeginners Nov. 12, 2013, 12:59 a.m. this will print the exception: except Exception, e: print str(e) or, more complete, with the name of the Exception and the value: except Exception, e: print repr(e)
Prachi Nov. 12, 2013, 12:44 a.m. Wht if i didn't know the excption? In the generic case, we