Beginning Python - Chapter7 : Exceptions

#Chapter8:Exceptions
#Question:Warning?
#1 The raise statement
# raise Exception('test')
import exceptions
print dir(exceptions) # built-in exceptions in the module exceptions(as well as in the built-in namespace)

# custom exception classes
class someCustomException(Exception):
    pass

# catch exceptions
try:
    x,y = 1,0
    print x/y #ZeroDivisionError: integer division or modulo by zero
except ZeroDivisionError:
    print 'the second number cannot be zero!'
   
# catch an exception and raise it again
class Test:
    muffled = False
    def f1(self,expr):
        try:
            return eval(expr)
        except ZeroDivisionError:
            if self.muffled:
                print '---1 division by zero is illegal'
            else:
                print '---2'
                raise
c = Test()
print c.f1('10/2')   #5
# print c.f1('10/0') # ---2 raise exception message
c.muffled = True
print c.f1('10/0')   # ---1 division ... None


#2 more than one except clause
try:
    x,y = 1,0
    z = 'hello'
    print x/z
except ZeroDivisionError:
    print 'cannot be zero'
except TypeError:
    print 'please use number'
except (ZeroDivisionError,TypeError): # cath two exceptions with one Block
    pass

#3 catch the object
try:
    x,y = 1,0
    print x/y
except ZeroDivisionError,e:
    print e # print exception message."integer division or modulo by zero"

#4 a real catch all
try:
    pass
except:
    pass # catch all kinds of exceptions

#5 else / break
#while True:
try:
        x,y=1,0
        print x/x
except:
        print 'something is wrong'
        print e
else:
        print 'everything is ok'
#       break #main use for loop
finally:
        print 'always show up'

#5 exceptions and functions
def faulty():
    raise Exception('--raise wrong')
def ignore_ex():
    faulty()
def handle_ex():
    try:
        faulty()
    except:
        print '--handle'

#ignore_ex() #Exception: --raise wrong
handle_ex()

#6 the zen of exceptions

File "/usr/lib/python2.7/site-packages/keystoneauth1/adapter.py", line 282, in get_endpoint 2023-05-22 12:11:00.079 10244 ERROR nova return self.session.get_endpoint(auth or self.auth, **kwargs) 2023-05-22 12:11:00.079 10244 ERROR nova File "/usr/lib/python2.7/site-packages/keystoneauth1/session.py", line 1218, in get_endpoint 2023-05-22 12:11:00.079 10244 ERROR nova return auth.get_endpoint(self, **kwargs) 2023-05-22 12:11:00.079 10244 ERROR nova File "/usr/lib/python2.7/site-packages/keystoneauth1/identity/base.py", line 380, in get_endpoint 2023-05-22 12:11:00.079 10244 ERROR nova allow_version_hack=allow_version_hack, **kwargs) 2023-05-22 12:11:00.079 10244 ERROR nova File "/usr/lib/python2.7/site-packages/keystoneauth1/identity/base.py", line 271, in get_endpoint_data 2023-05-22 12:11:00.079 10244 ERROR nova service_catalog = self.get_access(session).service_catalog 2023-05-22 12:11:00.079 10244 ERROR nova File "/usr/lib/python2.7/site-packages/keystoneauth1/identity/base.py", line 134, in get_access 2023-05-22 12:11:00.079 10244 ERROR nova self.auth_ref = self.get_auth_ref(session) 2023-05-22 12:11:00.079 10244 ERROR nova File "/usr/lib/python2.7/site-packages/keystoneauth1/identity/generic/base.py", line 208, in get_auth_ref 2023-05-22 12:11:00.079 10244 ERROR nova return self._plugin.get_auth_ref(session, **kwargs) 2023-05-22 12:11:00.079 10244 ERROR nova File "/usr/lib/python2.7/site-packages/keystoneauth1/identity/v3/base.py", line 188, in get_auth_ref 2023-05-22 12:11:00.079 10244 ERROR nova authenticated=False, log=False, **rkwargs) 2023-05-22 12:11:00.079 10244 ERROR nova File "/usr/lib/python2.7/site-packages/keystoneauth1/session.py", line 1124, in post 2023-05-22 12:11:00.079 10244 ERROR nova return self.request(url, 'POST', **kwargs) 2023-05-22 12:11:00.079 10244 ERROR nova File "/usr/lib/python2.7/site-packages/keystoneauth1/session.py", line 961, in request 2023-05-22 12:11:00.079 10244 ERROR nova raise exceptions.from_response(resp, method, url) 2023-05-22 12:11:00.079 10244 ERROR nova Unauthorized: The request you have made requires authentication. (HTTP 401) (Request-ID: req-009eabd5-6dd8-42fe-80ea-2fc398
最新发布
05-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值