《Python数据分析技术栈》第01章 04 语法错误和异常处理(Syntax errors and exceptions)

本文主要是介绍《Python数据分析技术栈》第01章 04 语法错误和异常处理(Syntax errors and exceptions),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

04 语法错误和异常处理(Syntax errors and exceptions)

《Python数据分析技术栈》第01章 04 语法错误和异常处理(Syntax errors and exceptions)

Syntax errors are errors that may be committed inadvertently by the user while writing the code, for example, spelling a keyword wrong, not indenting the code, and so on. An exception, on the other hand, is an error that occurs during program execution. A user may enter incorrect data while running the program. If you want to divide a number (say, ‘a’) by another number (say, ‘b’), but give a value of 0 to the denominator (‘b’), this will generate an exception. The exceptions, which are autogenerated in Python and displayed to the user, may not lucidly convey the problem. Using exception handling with the try-except construct, we can frame a user-friendly message to enable the user to better correct the error.

语法错误是指用户在编写代码时可能无意中犯下的错误,例如拼错关键字、没有缩进代码等。异常则是在程序执行过程中发生的错误。用户在运行程序时可能会输入错误的数据。如果你想用一个数(如 “a”)除以另一个数(如 “b”),但给分母(“b”)的值是 0,这就会产生异常。在 Python 中自动生成并显示给用户的异常可能无法清楚地表达问题。通过使用 try-except 结构的异常处理,我们可以编写出用户友好的信息,使用户能够更好地纠正错误。

There are two parts to exception handling. First, we put the code that is likely to cause an error under a try clause. Then, in the except clause, we try to deal with whatever caused an error in the try block. We mention the name of the exception class in the except clause, followed by a code block where we handle the error. A straightforward method for handling the error is printing a message that gives the user more details on what they need to correct.

异常处理分为两个部分。首先,我们将可能导致错误的代码放在 try 子句中。然后,在 except 子句中,我们尝试处理 try 代码块中导致错误的代码。我们在 except 子句中提及异常类的名称,然后在代码块中处理错误。处理错误的一种直接方法是打印一条消息,向用户提供需要更正的更多细节。

Note that all exceptions are objects that are derived from the class BaseException, and follow a hierarchy.

请注意,所有异常都是从 BaseException 类派生出来的对象,并遵循一个层次结构。

A simple example of a program, with and without exception handling, is shown below.

下面是一个简单的程序示例,包括异常处理和无异常处理两种情况。

while True:try:n = int(input('Enter your score:'))print('You obtained a score of ', n)breakexcept ValueError:print('Enter only an integer value')

Same program (Without exception handling):

n=int(input('Enter your score:'))
print('You obtained a score of ',n)

The statement that is likely to cause an error in the preceding code is: int(input(‘Enter your score:’)). The int function requires an integer as an argument. If the user enters a floating-point or string value, a ValueError exception is generated. When we use the tryexcept construct, the except clause prints a message asking the user to correct the input, making it much more explicit.

在前面的代码中,可能导致错误的语句是:int(input(‘输入您的分数:’))。int 函数需要一个整数作为参数。如果用户输入的是浮点数或字符串值,就会产生 ValueError 异常。当我们使用 tryexcept 结构时,except 子句会打印一条信息,要求用户更正输入内容,这样就更加明确了。

这篇关于《Python数据分析技术栈》第01章 04 语法错误和异常处理(Syntax errors and exceptions)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/620444

相关文章

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四

Python中win32包的安装及常见用途介绍

《Python中win32包的安装及常见用途介绍》在Windows环境下,PythonWin32模块通常随Python安装包一起安装,:本文主要介绍Python中win32包的安装及常见用途的相关... 目录前言主要组件安装方法常见用途1. 操作Windows注册表2. 操作Windows服务3. 窗口操作

Python中re模块结合正则表达式的实际应用案例

《Python中re模块结合正则表达式的实际应用案例》Python中的re模块是用于处理正则表达式的强大工具,正则表达式是一种用来匹配字符串的模式,它可以在文本中搜索和匹配特定的字符串模式,这篇文章主... 目录前言re模块常用函数一、查看文本中是否包含 A 或 B 字符串二、替换多个关键词为统一格式三、提

python常用的正则表达式及作用

《python常用的正则表达式及作用》正则表达式是处理字符串的强大工具,Python通过re模块提供正则表达式支持,本文给大家介绍python常用的正则表达式及作用详解,感兴趣的朋友跟随小编一起看看吧... 目录python常用正则表达式及作用基本匹配模式常用正则表达式示例常用量词边界匹配分组和捕获常用re

python实现对数据公钥加密与私钥解密

《python实现对数据公钥加密与私钥解密》这篇文章主要为大家详细介绍了如何使用python实现对数据公钥加密与私钥解密,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录公钥私钥的生成使用公钥加密使用私钥解密公钥私钥的生成这一部分,使用python生成公钥与私钥,然后保存在两个文

python删除xml中的w:ascii属性的步骤

《python删除xml中的w:ascii属性的步骤》使用xml.etree.ElementTree删除WordXML中w:ascii属性,需注册命名空间并定位rFonts元素,通过del操作删除属... 可以使用python的XML.etree.ElementTree模块通过以下步骤删除XML中的w:as

使用Python绘制3D堆叠条形图全解析

《使用Python绘制3D堆叠条形图全解析》在数据可视化的工具箱里,3D图表总能带来眼前一亮的效果,本文就来和大家聊聊如何使用Python实现绘制3D堆叠条形图,感兴趣的小伙伴可以了解下... 目录为什么选择 3D 堆叠条形图代码实现:从数据到 3D 世界的搭建核心代码逐行解析细节优化应用场景:3D 堆叠图

深度解析Python装饰器常见用法与进阶技巧

《深度解析Python装饰器常见用法与进阶技巧》Python装饰器(Decorator)是提升代码可读性与复用性的强大工具,本文将深入解析Python装饰器的原理,常见用法,进阶技巧与最佳实践,希望可... 目录装饰器的基本原理函数装饰器的常见用法带参数的装饰器类装饰器与方法装饰器装饰器的嵌套与组合进阶技巧

Python中Tensorflow无法调用GPU问题的解决方法

《Python中Tensorflow无法调用GPU问题的解决方法》文章详解如何解决TensorFlow在Windows无法识别GPU的问题,需降级至2.10版本,安装匹配CUDA11.2和cuDNN... 当用以下代码查看GPU数量时,gpuspython返回的是一个空列表,说明tensorflow没有找到