《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编写一个git自动上传的脚本(打包成exe)

《基于Python编写一个git自动上传的脚本(打包成exe)》这篇文章主要为大家详细介绍了如何基于Python编写一个git自动上传的脚本并打包成exe,文中的示例代码讲解详细,感兴趣的小伙伴可以跟... 目录前言效果如下源码实现利用pyinstaller打包成exe利用ResourceHacker修改e

Python在二进制文件中进行数据搜索的实战指南

《Python在二进制文件中进行数据搜索的实战指南》在二进制文件中搜索特定数据是编程中常见的任务,尤其在日志分析、程序调试和二进制数据处理中尤为重要,下面我们就来看看如何使用Python实现这一功能吧... 目录简介1. 二进制文件搜索概述2. python二进制模式文件读取(rb)2.1 二进制模式与文本

Python中Tkinter GUI编程详细教程

《Python中TkinterGUI编程详细教程》Tkinter作为Python编程语言中构建GUI的一个重要组件,其教程对于任何希望将Python应用到实际编程中的开发者来说都是宝贵的资源,这篇文... 目录前言1. Tkinter 简介2. 第一个 Tkinter 程序3. 窗口和基础组件3.1 创建窗

Django调用外部Python程序的完整项目实战

《Django调用外部Python程序的完整项目实战》Django是一个强大的PythonWeb框架,它的设计理念简洁优雅,:本文主要介绍Django调用外部Python程序的完整项目实战,文中通... 目录一、为什么 Django 需要调用外部 python 程序二、三种常见的调用方式方式 1:直接 im

Python字符串处理方法超全攻略

《Python字符串处理方法超全攻略》字符串可以看作多个字符的按照先后顺序组合,相当于就是序列结构,意味着可以对它进行遍历、切片,:本文主要介绍Python字符串处理方法的相关资料,文中通过代码介... 目录一、基础知识:字符串的“不可变”特性与创建方式二、常用操作:80%场景的“万能工具箱”三、格式化方法

Spring Boot 处理带文件表单的方式汇总

《SpringBoot处理带文件表单的方式汇总》本文详细介绍了六种处理文件上传的方式,包括@RequestParam、@RequestPart、@ModelAttribute、@ModelAttr... 目录方式 1:@RequestParam接收文件后端代码前端代码特点方式 2:@RequestPart接

SpringBoot全局异常拦截与自定义错误页面实现过程解读

《SpringBoot全局异常拦截与自定义错误页面实现过程解读》本文介绍了SpringBoot中全局异常拦截与自定义错误页面的实现方法,包括异常的分类、SpringBoot默认异常处理机制、全局异常拦... 目录一、引言二、Spring Boot异常处理基础2.1 异常的分类2.2 Spring Boot默

SpringBoot的全局异常拦截实践过程

《SpringBoot的全局异常拦截实践过程》SpringBoot中使用@ControllerAdvice和@ExceptionHandler实现全局异常拦截,@RestControllerAdvic... 目录@RestControllerAdvice@ResponseStatus(...)@Except

浅析python如何去掉字符串中最后一个字符

《浅析python如何去掉字符串中最后一个字符》在Python中,字符串是不可变对象,因此无法直接修改原字符串,但可以通过生成新字符串的方式去掉最后一个字符,本文整理了三种高效方法,希望对大家有所帮助... 目录方法1:切片操作(最推荐)方法2:长度计算索引方法3:拼接剩余字符(不推荐,仅作演示)关键注意事

python版本切换工具pyenv的安装及用法

《python版本切换工具pyenv的安装及用法》Pyenv是管理Python版本的最佳工具之一,特别适合开发者和需要切换多个Python版本的用户,:本文主要介绍python版本切换工具pyen... 目录Pyenv 是什么?安装 Pyenv(MACOS)使用 Homebrew:配置 shell(zsh