Chinaunix首页 | 论坛 | 博客
  • 博客访问: 201583
  • 博文数量: 64
  • 博客积分: 2536
  • 博客等级: 少校
  • 技术积分: 610
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-16 16:39
文章分类

全部博文(64)

文章存档

2011年(2)

2010年(1)

2009年(61)

我的朋友

分类: Python/Ruby

2009-09-22 10:36:03

#!/usr/bin/python
# Filename: try_except.py


import sys

try:
    s = raw_input('Enter something --> ')
except EOFError:
    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'

#!/usr/bin/python
# Filename: raising.py


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.

阅读(1119) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~