python的异常处理也是很简单的东西。
和其他语言的异常处理没什么太大的区别。
主要使用try...except 语句来进行的
import sys
try:
s = raw_input('Enter something --> ')
except SystemExit:
print '\nWhy did you do an EOF on me?'
sys.exit() # exit the program
except:
print '\nSome error/exception occurred.'
# here, we are not exiting the program
print 'Done'
使用ctrl+d来触发这个异常
class ShortInputException(Exception):
'''A user-defined exception class.'''
def __init__(self, length, atleast):
Exception.__init__(self)
self.length = length
self.atleast = atleast
try:
s = raw_input('Enter something --> ')
if len(s) < 3:
raise ShortInputException(len(s), 3)
# Other work can continue as usual here
except EOFError:
print '\nWhy did you do an EOF on me?'
except ShortInputException, x:
print 'ShortInputException: The input was of length %d, \
was expecting at least %d' % (x.length, x.atleast)
else:
print 'No exception was raised.'
下面是使用finlly和try...except嵌套
import time
try:
f = file('poem.txt')
while True: # our usual file-reading idiom
line = f.readline()
if len(line) == 0:
break
time.sleep(2)
print line,
except IOError:
print 'IOError'
finally:
try:
f.close()
except NameError:
print 'NameError'
print 'Cleaning up...closed the file'
分享到:
相关推荐
If you are already using Python for data analysis, you will find a number of things that you wish you knew how to do in Python. You can then take these techniques and apply them directly to your own ...
How do you take your data analysis skills beyond Excel to the next level? By learning just enough Python to get stuff done. This hands-on guide shows non-programmers like you how to process ...
Data is powerful but not in its raw form - It needs to be processed and modeled, and Python is one of the most robust tools out there to do so. It has an array of packages for predictive modeling and...
Then, the authors address the mechanics of the language itself, providing illustrations of how Python conceives of numbers, strings, and other objects as well as the operators you use to work with ...
how you endured your pre-Python days. About This Book Although Python is a great first programming language, in this book we do assume that you already have some programming experience. The first ...
- Environments for development - learn how you can take advantage of different tools for actually writing code, including those designed specifically for scientific work. - Organising and sharing code...
I am often asked the question: How do you use Python for machine learning? This book is my definitive answer to that question. It contains my very best knowledge and ideas on how to work through ...
You work in IT and need a programming primer – you might be a tester who needs to have more informed technical discussions with programmers. Working through the examples will help you to appreciate ...
In Automate the Boring Stuff with Python, you'll learn how to use Python to write programs that do in minutes what would take you hours to do by hand—no prior programming experience required....
As you do, you’ll learn how a computer works; what good programs look like; and how to read, write, and think about code. Zed then teaches you even more in 5+ hours of video where he shows you how ...
Machine Learning in Python shows you how to do this, without requiring an extensive background in math or statistics. Table of Contents Chapter 1 The Two Essential Algorithms for Making Predictions ...
especially how built-in objects like dictionaries and strings work, have changed considerably, and a lot of deprecated features have finally been removed. Build Instructions ------------------ On ...
This book is intended for Python programmers interested in learning how to do natural language processing. Maybe you've learned the limits of regular expressions the hard way, or you've realized that ...
made me realize how much I enjoyed working with Python, so naturally I had to start using GDAL with it as well. More importantly for this book, my coworker John Lowry suggested that we team- teach a ...
Python is easy to learn and extensible programming language that allows any manner of secret agent to work with a variety of data. Agents from beginners to seasoned veterans will benefit from Python's...
Environments for development - learn how you can take advantage of different tools for actually writing code, including those designed specifically for scientific work. Organising and sharing code - ...
Machine Learning in Python: Essential Techniques for Predictive Analysis ... Machine Learning in Python shows you how to do this, without requiring an extensive background in math or statistics.
Computer vision is found everywhere in ... By the end of this chapter, you will be able to understand how neural networks work and how to apply them to machine learning to build advance images tools.
We will also do some biology and talk about how convolutional neural networks have been inspired by the animal visual cortex. After describing the architecture of a convolutional neural network, we ...
If this is your life, this book will make it better – a practical guide on how to do what you want with textual data in Python. Python 2.6 Text Processing Beginner’s Guide is the easiest way to ...