`

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

    Python 2.1 Bible .pdf

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

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

    Lean.Python.Learn.Just.Enough.Python.to.Build.Useful.Tools

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

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

    笨方法学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 ...

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

    Python 3 Text Processing with NLTK 3 Cookbook

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

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

    Python.for.Secret.Agents.2nd.Edition.1785283405

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

    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.

    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.

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

    Packtpub.Python.2.6.Text.Processing.Beginners.Guide.Dec.2010

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

Global site tag (gtag.js) - Google Analytics