当前位置:网站首页>[song] rebirth of me in py introductory training (9): exception handling
[song] rebirth of me in py introductory training (9): exception handling
2022-07-27 06:05:00 【Collapse an old face】
It's not easy to create , Before reference , Point a praise , Collection , You can't pay too much attention , Family
The first 1 Turn off :Python Exception class and custom exception
Task description
Skilled use of exception handling mechanism plays an important role in improving the robustness and fault tolerance of programs , At the same time, you can put Python Obscure error prompts are converted into friendly prompts and displayed to end users . The task of this chapter is to let readers deeply understand the meaning of exceptions , And master the method of customizing exceptions .
#### Related knowledge
##### The basic concept of exceptions
What on earth is abnormal ? The following example can enlighten you :
x, y = 12, 5a = x / yprint(A) # Spelling mistakes ,Python Case sensitive , Variables are not defined A
----------------------------------------------------------
Programming requirements
Customize as required MyError Exception class , Properly MyError When an exception occurs , The execution evaluation code can output the following information .
Test input :4
Expected output : This is my definition of the second 4 Exceptions
# -*- coding: utf-8 -*-
class MyError(Exception):
#********begin*********#
def __init__(self,value):
self.value = value
def __str__(self):
return ' This is my definition of the second %d Exceptions '%(self.value)
#******** end*********#The first 2 Turn off :Python Exception handling structure in
Task description
The task of this level is to let students learn how to apply exception handling methods in the program , master Python Exception handling structure in .
#### Related knowledge
#####try...except... structure
The most common and basic exception handling structure is try...except... structure .try The code block in the clause contains statements that may have exceptions , and except Clause is used to catch the corresponding exception ,except The code block in the clause is used to handle exceptions .
-------------------------------------------------------------------------------------
Programming requirements
Complete the code on the right as required . To achieve the following functions :
- When the length of the test input string is greater than
3Throw whenMyErrorabnormal , And the output length is too long , Greater than3Otherwise, the output length is appropriate . - And whether or not an exception is thrown , After the execution is completed, output the execution completion .
#*********begin*********# try: if(x>3): raise MyError() except MyError as e: print(e) else: print(' The length is right ') finally: print(' completion of enforcement ') #********* end*********#P.s: This level is difficult , I paid a lot of money , Give me a compliment + Pay attention to reincarnation
notes : The content is only for reference and sharing , Do not spread without permission , Tort made delete
边栏推荐
- 维度问题以及等高线
- Weidongshan digital photo frame project learning (II) displaying Chinese characters on LCD
- Redis在windows下的idea连接不上问题
- 韦东山 数码相框 项目学习(一)在LCD上显示ASCII字符
- Kaggle调用自定义模块方法
- 【头歌】重生之数据科学导论——回归进阶
- 10. Gradient, activation function and loss
- [song] rebirth of me in py introduction training (5): List
- Greedy high performance neural network and AI chip application research and training
- 9. High order operation
猜你喜欢

4. Tensor data type and creation tensor

pytorch使用data_prefetcher提升数据读取速度

Matlab 画图(超详细)

leetcode系列(一):买卖股票

面试常问Future、FutureTask和CompletableFuture

Digital image processing -- Chapter 9 morphological image processing

Speech and Language Processing (3rd ed. draft) Chapter 2 ——正则表达式,文本归一化,编辑距离 阅读笔记

物联网操作系统多任务基础

Day 3. Suicidal ideation and behavior in institutions of higher learning: A latent class analysis
![[concurrent programming series 9] priorityblockingqueue, delayqueue principle analysis of blocking queue](/img/2b/1bfadbffad33f8560357fab74e8308.png)
[concurrent programming series 9] priorityblockingqueue, delayqueue principle analysis of blocking queue
随机推荐
百问网驱动大全学习(一)LCD驱动
【头歌】重生之我在py入门实训中(3): if条件语句
【头歌】重生之我在py入门实训中(12):Matplotlib接口和常用图形
编程学习记录——递归解决汉诺塔问题
pytorch中交叉熵损失函数的细节
剪枝-量化-转onnx中文系列教程
关于druid连接不上数据库的问题
Essential tool for making video special effects: nuke 13
pytorch的多GPU训练的两种方式
Greedy high performance neural network and AI chip application research and training
operator() 用法之一
韦东山 数码相框 项目学习(四)简易的TXT文档显示器(电纸书)
【12】理解电路:从电报机到门电路,我们如何做到“千里传信”?
15. GPU acceleration, Minist test practice and visdom visualization
AE 3D particle system plug-in: Trapcode particle
socket编程一:使用fork()实现最基础的并发模式
QGIS系列(1)-QGIS(server-apache) win10安装
【头歌】重生之我在py入门实训中(1)
Stm32-fsmc extended memory SRAM
【11】二进制编码:“手持两把锟斤拷,口中疾呼烫烫烫”?