`

Python,how do you work (三)

 
阅读更多

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'

 

 

分享到:
评论

相关推荐

    Learn Data Analysis with Python 英文原版

    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 ...

    Data Wrangling with Python 2016新书 无水印pdf 0分

    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 ...

    Python Advanced Predictive Analytics

    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...

    Learning Python

    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 ...

    [machine_learning_mastery系列]Machine_Learning_Mastery_With_Python.pdf

    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 ...

    Automate.the.Boring.Stuff.with.Python.Practical.Programming

    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....

    Geoprocessing-with-Python.pdf

    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 ...

    笨方法学python3 Learn Python 3 the Hard Way

    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 ...

    OpenCV 3.x with Python By Example, 2nd Edition-Packt Publishing(2018).pdf

    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.

    Machine.Learning.in.Python

    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 ...

    Python安装包version 3.1.5

    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 ...

    Effective Python Development for Biologists pdf

    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 无水印pdf 0分

    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.

    Udemy - Deep Learning Convolutional Neural Networks in Python

    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 ...

    Rapid GUI Programming with Python and Qt

    This book teaches how to write GUI applications using the Python programming language and the Qt application development framework. The only prior knowledge assumed is that you can program in some ...

    Udemy - Deep Learning Recurrent Neural Networks in Python

    You saw when we studied Markov Models that we could do things like generate poetry and it didn’t look too bad. We could even discriminate between 2 different poets just from the sequence of parts-of-...

Global site tag (gtag.js) - Google Analytics