python捕获异常的函数_python-内置函数及捕获异常

eval:把字符串转换成有效表达式

repr:把有效表达式转换成字符串

round(...)

round(number[, ndigits]) -> number

Round a number to a given precision in decimal digits (default 0 digits).

This returns an int when called with one argument, otherwise the

same type as the number. ndigits may be negative.

pow(x, y, z=None, /)

Equivalent to x**y (with two arguments) or x**y % z (with three arguments)

Some types, such as ints, are able to use a more efficient algorithm when

invoked using the three argument form.

class map(object)

|  map(func, *iterables) --> map object

|

|  Make an iterator that computes the function using arguments from

|  each of the iterables.  Stops when the shortest iterable is exhausted.

>>> a

[1, 2, 3]

>>> b

['a', 'b', 'c']

>>> dict(zip(a,b))

{1: 'a', 2: 'b', 3: 'c'}

>>> mylist.extend([1,2,3])

>>> mylist

[2, 1, 3, 2, 1, 4, 3, 2, 1, 'a', 'a', 1, 2, 3]

>>> mylist.append([1,2,3])

>>> mylist

[2, 1, 3, 2, 1, 4, 3, 2, 1, 'a', 'a', 1, 2, 3, [1, 2, 3]]

-------------------------------------------以上是内置函数------------------------------------------

异常,错误

程序没法处理的任务,会抛出异常

错误一般是,逻辑错误,语法错误,无法生成结果等

>>> a

Traceback (most recent call last):

File "", line 1, in

NameError: name 'a' is not defined

>>> 1 / 0

Traceback (most recent call last):

File "", line 1, in

ZeroDivisionError: division by zero

>>> if a = b

File "", line 1

if a = b

^

SyntaxError: invalid syntax

>>> b[4]

Traceback (most recent call last):

File "", line 1, in

IndexError: list index out of range

在python中,大部分异常都是基于Exception这个类

KeyboardInterruptException  exit

基类

Exception

python可以通过

try语句检测异常:

要检测异常的语句

except语句来处理异常:

>>> try:

...     print(a)

... except NameError:

...     print('ABC')

...

ABC

>>> try:

...     print(a)

... except Exception as error:

...     print(error)

...

name 'a' is not defined

try:

要检测异常的语句

except检测的异常(如果我们使用Execption)则是捕捉所有异常:#但是不建议大家这么做,因为有些异常是我们需要去真实看到的,

执行捕获异常之后的事情

else:

用来在没有任何异常情况下执行这里的内容

finally:

不管异常是否发生,这里面写的语句都会执行

我们一般用来关闭socket,关闭文件,回收线程,进程创建释放,数据库连接,mysql句柄,做一些对象内存释放的工作,

>>> try:

...     print(a)

... except Exception:

...     print('abc')

... else:

...     print('--------')

... finally:

...     print('********')

...

abc

********

raise可以抛出异常

断言:

assert当判断的语句为false那么会抛出一个AssertionError异常

断言一般用来判断一些bool语句,在断言成功时不采取任何措施,否则触发AssertionError(断言错误)的异常,

try:

assert 1 == 0

except AssertionError:

print('not equal')

with语句,with语句其实和try,finally语句是一样的,只不过,他只对支持上下文管理协议(context

management protocol),比如我们的mysql链接数据库,会在完成业务之后他有关闭,打开文件也会有关闭文件,

close()

我们通过with语句打开一个文件对象,如果没有出错,文件对象就是file,

之后我们做的一系列操作不论是否会发生错误,抛出异常,都会执行内存清理的语句,刷新缓冲区,关闭文件等

-----------------------------------------------异常------------------------------------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值