Skip to content
Navigation Menu
登录
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
操作
Automate any workflow
代码空间
Instant dev environments
问题
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced 安全
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
市场
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
安全
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub 赞助
Fund open source developers
PROGRAMS
安全 Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced 安全
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
搜索
/
登录
注册
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
python-cloud
/
python_test
Public
forked from
SergioJune/python_test
通知
You must be signed in to change notification settings
复刻
2
Star
0
Code
拉取请求
0
操作
项目
安全 and quality
0
洞察
Additional navigation options
Code
拉取请求
操作
项目
安全 and quality
洞察
Files
Expand file tree
master
Breadcrumbs
python_test
/
test26.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
83 lines (63 loc) · 1.74 KB
master
Breadcrumbs
python_test
/
test26.py
Copy path
Top
File metadata and controls
Code
Blame
83 lines (63 loc) · 1.74 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'练习错误处理'
__author__ = 'sergiojune'
import logging
from functools import reduce
def foo(n):
# 自定义抛出错误
if n == 0:
raise ZeroDivisionError('被除数不能为零')
return 10 / n
print(foo(20))
# 这个就会出错
# print(foo(0))
# 捕捉错误
try:
n = input('请输入一个数字')
n = int(n)
res = 10 / n
print(res)
# 有多种异常情况,所以有多个except语句块
except ZeroDivisionError as e:
print(e)
except ValueError as e:
# 用这个记录错误,开发中会发在日志上查看
logging.exception(e)
# 当没有发生错误时会执行
else:
print('成功运行')
# 这个语句块是必须执行的
finally:
print('代码执行完毕')
print('end')
# 还可以自己创建异常
# 只需要继承自某一个异常就可以了,不过一般不需要自定义异常
class FooException(BaseException):
pass
# 作业:运行下面的代码,根据异常信息进行分析,定位出错误源头,并修复
def str2num(s):
try:
return int(s)
except ValueError as e:
logging.exception(e)
print('捕捉成功')
try:
return float(s)
except ValueError as e:
print(e)
print('输入的内容不是数字')
def calc(exp):
try:
ss = exp.split('+')
ns = map(str2num, ss)
return reduce(lambda acc, x: acc + x, ns)
except TypeError as e:
print(e)
def main():
r = calc('100 + 200 + 345')
print('100 + 200 + 345 =', r)
r = calc('99 + 88 + 7.6')
print('99 + 88 + 7.6 =', r)
main()
print('end')
You can’t perform that action at this time.