What is the difference between try/except and assert?
assert only check if a condition is true or not and throw an exception. A try/except block can run a few statements and check if any of them throw an exception so you can process it in the exceptpart. Examples:
assert(1 == 2)
will give you an AsertionError.
try:
# some statements
# ...
except OSError as err:
#If an OSerror exception is thrown, you can process it here. For example:
print("OS error: {0}".format(err))
Your code will look like this:
def function_addition(x,y):
try:
assert (y!=0)
except:
raise ValueError('y is 0.')
total= x/y
return total
num1=float(input("Write a number :"))
num2=float (input("Write a second number:"))
try:
result=function_addition(num1,num2)
except ValueError as ve:
print(ve)
else:
print(result)
If you save it in a fun.py file and run it, you will have this output:
Write a number :1
Write a second number:2
0.5
# Run it again.
Write a number :0
Write a second number:0
y is 0.
What is the difference between try/except and assert?的更多相关文章
- Deep Learning 12_深度学习UFLDL教程:Sparse Coding_exercise(斯坦福大学深度学习教程)
前言 理论知识:UFLDL教程.Deep learning:二十六(Sparse coding简单理解).Deep learning:二十七(Sparse coding中关于矩阵的范数求导).Deep ...
- hadoop源码_hdfs启动流程_3_心跳机制
hadoop在启动namenode和datanode之后,两者之间是如何联动了?datanode如何向namenode注册?如何汇报数据?namenode又如何向datanode发送命令? 心跳机制基 ...
- 『德不孤』Pytest框架 — 8、Pytest断言
目录 1.什么是断言 2.Pytest断言 3.Pytest的断言方式及应用场景 (1)使用assert语句 (2)断言预期的异常 (3)拓展 4.优化断言 5.使用标记检查异常 1.什么是断言 对于 ...
- Java 堆内存与栈内存异同(Java Heap Memory vs Stack Memory Difference)
--reference Java Heap Memory vs Stack Memory Difference 在数据结构中,堆和栈可以说是两种最基础的数据结构,而Java中的栈内存空间和堆内存空间有 ...
- What's the difference between a stub and mock?
I believe the biggest distinction is that a stub you have already written with predetermined behavio ...
- [转载]Difference between <context:annotation-config> vs <context:component-scan>
在国外看到详细的说明一篇,非常浅显透彻.转给国内的筒子们:-) 原文标题: Spring中的<context:annotation-config>与<context:componen ...
- What's the difference between <b> and <strong>, <i> and <em> in HTML/XHTML? When should you use each?
ref:http://stackoverflow.com/questions/271743/whats-the-difference-between-b-and-strong-i-and-em The ...
- difference between forward and sendredirect
Difference between SendRedirect and forward is one of classical interview questions asked during jav ...
- Add Digits, Maximum Depth of BinaryTree, Search for a Range, Single Number,Find the Difference
最近做的题记录下. 258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the ...
随机推荐
- POJ1111【BFS】
在搜1011的时候误搜了1111,简单BFS吧,多一个X就是多四个面,每次看看他的四个面有多少个重复的,然后剪掉,最后答案加上就好了: code: //#include <bits/stdc++ ...
- Unity IK(反向运动学)初探
http://blog.csdn.net/myarrow/article/details/44450199 1. 简介 IK与FK对应,正向运动学就是根骨骼带动节点骨骼运动.而反向运动学就是反过来,由 ...
- Python 3.x 的一些注意事项
1. reload 被更改 需要 在console执行 from imp import reload 才能调用CT 同时,如果py文件是位于主文件夹深部的位置,可以这么做: import ComicT ...
- elasticsearch 备份和恢复
curl : http://keenwon.com/1393.html During snapshot initialization, information about all previous ...
- Centos7安装与配置domain模式wildfly(默认配置)
(1)安装与配置JDK8 1)使用wget下载JDK8: wget --no-check-certificate --no-cookies --header "Cookie: oraclel ...
- 081 Search in Rotated Sorted Array II 搜索旋转排序数组 ||
这是 “搜索旋转排序数组”问题的跟进:如果数组元素允许重复,怎么办?这会影响到程序的时间复杂度吗?会有怎样的影响,为什么?假设按照升序排序的数组在预先未知的某个关键点上旋转.(例如, 0 1 2 4 ...
- Github开源项目单
以下涉及到的数据统计与 2019 年 5 月 1 日 12 点,数据来源:https://github.com/trending/java?since=monthly . 下面的内容从 Java 学习 ...
- CentOS 7.4升级curl和git到最新版本
升级curl和git到最新版本 [root@jenkins ~]# yum install -y curl-devel expat-devel gettext-devel openssl-devel ...
- MFC双缓冲解决图象闪烁[转]
转载网上找到的一篇双缓冲的文章,很好用.http://www.cnblogs.com/piggger/archive/2009/05/02/1447917.html__________________ ...
- Android中的Selector的使用总结
Android中的Selector主要是用来改变ListView和Button控件等其他空的默认背景,其使用方法可以如下所示: 1.首先在res目录下drawable文件夹,新建一个comm_butt ...