英文:
Expectation handling in python
问题
以下是两个函数定义:
def function1():
try:
# 代码
# 代码
# 此处是读取文件的代码
# 代码
return smth
except Exception as e:
raise Exception('smth1smth1') from e
def function2():
try:
# 代码
smth = function1()
data = {# 一些 JSON 键值对}
data2 = request.post(smth123, data)
# 代码
return smth
except Exception as e:
print('error:%s, error:%s' % (data.key1, data.key2, str(e)))
raise Exception('smth2smth2') from e
对于这个测试案例,文件在这里是缺失的。我期望会得到文件丢失的异常,但由于变量"data"未被赋值,我也会得到在赋值之前引用变量的错误。我不想看到从第二个函数即function2抛出的异常(错误),而是只想看到从第一个函数即function1抛出的异常,如果有的话。如何实现这一点?
英文:
Below are two function definition
def function1():
try:
#Code
#Code
#code to read file is here
#code
return smth
except Exception as e:
raise Exception ('smth1smth1') from e
def function2():
try:
#Code
smth=function1()
data= {#some json key value pairs}
data2=request.post(smth123,data)
#code
return smth
except Exception as e:
print('error:%s, error:%s , %(data.key1, data.key2, str(e)))
raise Exception ('smth2smth2') from e
The test case for this is the file is missing here. I am getting the file missing exception as expected but as variable "data" is not assigned I am also getting the variable referenced before assignment error as well.
I don't want to see the exception (error) thrown from the second function i.e function 2 and see the exception from the first i.e. function1 if there is any. How can I achieve this
答案1
得分: 1
在你的except
块中的代码不应该依赖于可能在try
块中失败的任何内容。所以,你可以在except
中不使用data
:
def function2():
try:
#代码
smth = function1()
data = {#一些JSON键值对}
data2 = request.post(smth123, data)
#代码
return smth
except Exception as e:
print('错误:', e)
raise Exception('smth2smth2') from e
或者你可以使用多个try
块:
def function2():
try:
#代码
smth = function1()
except Exception as e:
print('错误:%s,错误:%s' % (data.key1, data.key2, str(e)))
raise Exception('smth2smth2') from e
try:
data = {#一些JSON键值对}
data2 = request.post(smth123, data)
#代码
return smth
except Exception as e:
print('错误:%s,错误:%s' % (data.key1, data.key2, str(e)))
raise Exception('smth2smth2') from e
或者你可以使用不同的Exception
类,在单个try
中有多个except
块,每个匹配特定类别的异常(假设每个异常可能在try
的不同部分引发):
class Function1Exception(Exception):
pass
def function1():
try:
#代码
return 'smth'
except Exception as e:
raise Function1Exception('smth1smth1') from e
def function2():
try:
#代码
smth = function1()
data = {'key': 'value'}
data2 = request.post(smth, data)
#代码
return smth
except Function1Exception as e:
print('function1失败:', e)
except RequestException as e:
print(f'request失败: {data.key1}, {data.key2}, 错误: {e}')
raise Exception('smth2smth2') from e
请注意,如果你在function1
中根本不进行异常封装,那么你将自动获得一个更有用的异常类,如FileNotFoundError
,就像request.post
在失败时会引发RequestException
一样,而不是任何旧的Exception
。重新引发更通用的异常(即普通的Exception
)实际上会丢失信息,使错误处理变得更加困难。
英文:
The code in your except
block needs to not have dependencies on anything that might have failed in the try
block. So you either need to not have data
in the except
:
def function2():
try:
#Code
smth=function1()
data= {#some json key value pairs}
data2=request.post(smth123,data)
#code
return smth
except Exception as e:
print('error:', e)
raise Exception('smth2smth2') from e
or you need multiple try
blocks:
def function2():
try:
#Code
smth=function1()
except Exception as e:
print('error:%s, error:%s , %(data.key1, data.key2, str(e)))
raise Exception('smth2smth2') from e
try:
data= {#some json key value pairs}
data2=request.post(smth123,data)
#code
return smth
except Exception as e:
print('error:%s, error:%s , %(data.key1, data.key2, str(e)))
raise Exception('smth2smth2') from e
Or you can use different Exception
classes to have multiple except
blocks in a single try
, each matching a specific class of exception (with the assumption being that each might get raised in a different part of the try
):
class Function1Exception(Exception):
pass
def function1():
try:
#code
return 'smth'
except Exception as e:
raise Function1Exception('smth1smth1') from e
def function2():
try:
#Code
smth = function1()
data = {'key': 'value'}
data2 = request.post(smth, data)
# code
return smth
except Function1Exception as e:
print('function1 failed:', e)
except RequestException as e:
print(f'request failed: {data.key1}, {data.key2}, error: {e}')
raise Exception('smth2smth2') from e
Note that if you weren't doing that except-wrapping in function1
at all, you would automatically get a more useful exception class like FileNotFoundError
, the same way request.post
will raise a RequestException
on a failure rather than any old Exception
. Re-raising a more generic exception (i.e. a regular Exception
) is actually losing information and making it harder to handle the error.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论