python负数错误异常类型_异常和异常类(Exception and Exception Classes)

异常和异常类(Exception and Exception Classes)

通常,例外是任何异常情况。 例外通常表示错误,但有时他们故意放入程序,例如提前终止程序或从资源短缺中恢复。 有许多内置异常,表示读取文件末尾或除以零等条件。 我们可以定义自己的异常,称为自定义异常。

异常处理使您可以优雅地处理错误并对其执行有意义的操作。 异常处理有两个组成部分:“抛出”和“捕获”。

Identifying Exception (Errors)

Python中发生的每个错误都会导致异常,这个异常将由其错误类型识别出错误条件。>>> #Exception

>>> 1/0

Traceback (most recent call last):

File "", line 1, in

1/0

ZeroDivisionError: division by zero

>>>

>>> var = 20

>>> print(ver)

Traceback (most recent call last):

File "", line 1, in

print(ver)

NameError: name 'ver' is not defined

>>> #Above as we have misspelled a variable name so we get an NameError.

>>>

>>> print('hello)

SyntaxError: EOL while scanning string literal

>>> #Above we have not closed the quote in a string, so we get SyntaxError.

>>>

>>> #Below we are asking for a key, that doen't exists.

>>> mydict = {}

>>> mydict['x']

Traceback (most recent call last):

File "", line 1, in

mydict['x']

KeyError: 'x'

>>> #Above keyError

>>>

>>> #Below asking for a index that didn't exist in a list.

>>> mylist = [1,2,3,4]

>>> mylist[5]

Traceback (most recent call last):

File "", line 1, in

mylist[5]

IndexError: list index out of range

>>> #Above, index out of range, raised IndexError.

Catching/Trapping Exception

当程序中出现异常并且您希望使用异常机制处理它时,您会“抛出异常”。 关键字try和except用于捕获异常。 每当try块中发生错误时,Python都会查找匹配的except块来处理它。 如果有,执行跳转到那里。

句法try:

#write some code

#that might throw some exception

except :

# Exception handler, alert the user

try子句中的代码将逐语句执行。

如果发生异常,将跳过try块的其余部分并执行except子句。try:

some statement here

except:

exception handling

让我们编写一些代码,看看在程序中不使用任何错误处理机制时会发生什么。number = int(input('Please enter the number between 1 & 10: '))

print('You have entered number',number)

只要用户输入一个数字,上面的程序就能正常工作,但是如果用户试图放入一些其他数据类型(如字符串或列表)会发生什么。Please enter the number between 1 > 10: 'Hi'

Traceback (most recent call last):

File "C:/Python/Python361/exception2.py", line 1, in

number = int(input('Please enter the number between 1 & 10: '))

ValueError: invalid literal for int() with base 10: "'Hi'"

现在ValueError是一种异常类型。 让我们尝试用异常处理重写上面的代码。import sys

print('Previous code with exception handling')

try:

number = int(input('Enter number between 1 > 10: '))

except(ValueError):

print('Error..numbers only')

sys.exit()

print('You have entered number: ',number)

如果我们运行程序,并输入一个字符串(而不是数字),我们可以看到我们得到了不同的结果。Previous code with exception handling

Enter number between 1 > 10: 'Hi'

Error..numbers only

提高例外

要从您自己的方法中提出异常,您需要使用像这样的raise关键字raise ExceptionClass(‘Some Text Here’)

我们来举个例子吧def enterAge(age):

if age<0:

raise ValueError('Only positive integers are allowed')

if age % 2 ==0:

print('Entered Age is even')

else:

print('Entered Age is odd')

try:

num = int(input('Enter your age: '))

enterAge(num)

except ValueError:

print('Only positive integers are allowed')

运行程序并输入正整数。

预期产出Enter your age: 12

Entered Age is even

但是当我们尝试输入负数时,我们得到了

预期产出Enter your age: -2

Only positive integers are allowed

创建自定义异常类

您可以通过扩展BaseException类或BaseException的子类来创建自定义异常类。

cf49cc02f0d3954d9472ebd618703616.png

从上图中我们可以看到Python中的大多数异常类都是从BaseException类扩展而来的。 您可以从BaseException类或其子类派生自己的异常类。

创建一个名为NegativeNumberException.py的新文件并编写以下代码。class NegativeNumberException(RuntimeError):

def __init__(self, age):

super().__init__()

self.age = age

上面的代码创建了一个名为NegativeNumberException的新异常类,它只包含使用super()__ init __()调用父类构造函数的构造函数,并设置年龄。

现在要创建自己的自定义异常类,将编写一些代码并导入新的异常类。from NegativeNumberException import NegativeNumberException

def enterage(age):

if age < 0:

raise NegativeNumberException('Only positive integers are allowed')

if age % 2 == 0:

print('Age is Even')

else:

print('Age is Odd')

try:

num = int(input('Enter your age: '))

enterage(num)

except NegativeNumberException:

print('Only positive integers are allowed')

except:

print('Something is wrong')

输出 (Output)Enter your age: -2

Only positive integers are allowed

另一种创建自定义Exception类的方法。class customException(Exception):

def __init__(self, value):

self.parameter = value

def __str__(self):

return repr(self.parameter)

try:

raise customException('My Useful Error Message!')

except customException as instance:

print('Caught: ' + instance.parameter)

输出 (Output)Caught: My Useful Error Message!

异常层次结构

内置异常的类层次结构是 -+-- SystemExit

+-- KeyboardInterrupt

+-- GeneratorExit

+-- Exception

+-- StopIteration

+-- StopAsyncIteration

+-- ArithmeticError

| +-- FloatingPointError

| +-- OverflowError

| +-- ZeroDivisionError

+-- AssertionError

+-- AttributeError

+-- BufferError

+-- EOFError

+-- ImportError

+-- LookupError

| +-- IndexError

| +-- KeyError

+-- MemoryError

+-- NameError

| +-- UnboundLocalError

+-- OSError

| +-- BlockingIOError

| +-- ChildProcessError

| +-- ConnectionError

| | +-- BrokenPipeError

| | +-- ConnectionAbortedError

| | +-- ConnectionRefusedError

| | +-- ConnectionResetError

| +-- FileExistsError

| +-- FileNotFoundError

| +-- InterruptedError

| +-- IsADirectoryError

| +-- NotADirectoryError

| +-- PermissionError

| +-- ProcessLookupError

| +-- TimeoutError

+-- ReferenceError

+-- RuntimeError

| +-- NotImplementedError

| +-- RecursionError

+-- SyntaxError

| +-- IndentationError

| +-- TabError

+-- SystemError

+-- TypeError

+-- ValueError

| +-- UnicodeError

| +-- UnicodeDecodeError

| +-- UnicodeEncodeError

| +-- UnicodeTranslateError

+-- Warning

+-- DeprecationWarning

+-- PendingDeprecationWarning

+-- RuntimeWarning

+-- SyntaxWarning

+-- UserWarning

+-- FutureWarning

+-- ImportWarning

+-- UnicodeWarning

+-- BytesWarning

+-- ResourceWarning

文件和字符串(Files and Strings)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值