说到Python,我是个新手。我在一个azure sql笔记本中有一些python代码,它由一个计划的作业运行。job notebook运行几个sql notebook。如果第一个notebook出错,我希望抛出一个异常,以便计划的作业显示为失败,并且我不希望运行后续的sql notebook。python代码如下所示
%python
try:
dbutils.notebook.run("/01. SMETS1Mig/" + dbutils.widgets.get("env_parent_directory") + "/02 Processing Curated Staging/02 Build - Parameterised/STA_1A - CS note Issued", 6000, {
"env_ingest_db": dbutils.widgets.get("env_ingest_db")
, "env_stg_db": dbutils.widgets.get("env_stg_db")
, "env_tech_db": dbutils.widgets.get("env_tech_db")
})
except :
print ' Failure in STA_1S'
try:
dbutils.notebook.run("/01. SMETS1Mig/" + dbutils.widgets.get("env_parent_directory") + "/02 Processing Curated Staging/02 Build - Parameterised/Output CS Notes", 6000, {
"env_ingest_db": dbutils.widgets.get("env_ingest_db")
, "env_stg_db": dbutils.widgets.get("env_stg_db")
, "env_tech_db": dbutils.widgets.get("env_tech_db")
})
except :
print ' Error in Output CS Notes'我的方向是对的吗?
实现这一目标的最好方法是什么?
非常感谢你提前
发布于 2020-06-24 18:24:37
选项1:简单地不捕捉异常。只需在没有try ... except的情况下运行第一个命令,并让异常以正常方式冒泡。如果它被引发(即抛出),那么第二个命令(或它之后的任何内容)将不会运行。然后,您只能使用第二个命令捕获异常。
dbutils.notebook.run(...first invocation...) # exceptions not caught - will bubble
try:
dbutils.notebook.run(...second invocation...)
except Exception as exc:
print(' Error in Output CS Notes: {}'.format(exc))选项2:如果您想自己进行某种处理,那么一定要捕获异常,但是在except块中重新raise它(或另一个异常)。
try:
dbutils.notebook.run(...first invocation...)
except Exception as exc:
print(' Failure in STA_1S: {}'.format(exc))
raise exc
# or for example: raise RuntimeError
# ... then do as before with second invocation...(您也可以使用不带任何参数的raise而不是raise exc,因为它将默认重新引发原始异常。)
发布于 2020-06-24 18:24:57
设置一个标志,显示第一个notebook是否成功:
first_notebook_succeeded = False # assume failure
try:
dbutils.notebook.run("/01. SMETS1Mig/" + dbutils.widgets.get("env_parent_directory") + "/02 Processing Curated Staging/02 Build - Parameterised/STA_1A - CS note Issued", 6000, {
"env_ingest_db": dbutils.widgets.get("env_ingest_db")
, "env_stg_db": dbutils.widgets.get("env_stg_db")
, "env_tech_db": dbutils.widgets.get("env_tech_db")
})
except :
print ' Failure in STA_1S'
else:
first_notebook_succeeded = True
if first_notebook_succeeded: # only run if first notebook ran OK
try:
dbutils.notebook.run("/01. SMETS1Mig/" + dbutils.widgets.get("env_parent_directory") + "/02 Processing Curated Staging/02 Build - Parameterised/Output CS Notes", 6000, {
"env_ingest_db": dbutils.widgets.get("env_ingest_db")
, "env_stg_db": dbutils.widgets.get("env_stg_db")
, "env_tech_db": dbutils.widgets.get("env_tech_db")
})
except :
print ' Error in Output CS Notes'当然,您也可以将first_notebook_succeeded初始设置为True,如果第一个notebook出现异常,则将其设置为False。
发布于 2020-06-24 18:22:02
对不起-我没有仔细阅读这个问题。
我最初的答案不是你想要的。
我写道:
添加一条
return语句:
尝试:# ...Exception Exception as error: print f‘’Failure in STA_1S ({error})‘return
但你不仅想离开函数,实际上还想以一个错误代码结束它的执行。
你要找的是sys.exit()
退出Python。这是通过引发SystemExit异常来实现的,因此由try语句的finally子句指定的清理操作将得到遵守,并且可以在外部级别拦截退出尝试。
可选参数arg可以是给定退出状态的整数(默认为0),也可以是另一种类型的对象。如果它是一个整数,则零被认为是“成功终止”,任何非零值被shell等认为是“异常终止”。大多数系统要求它在0-127的范围内,否则会产生未定义的结果。一些系统有为特定退出代码指定特定含义的约定,但这些约定通常开发不足;Unix程序通常使用2表示命令行语法错误,使用1表示所有其他类型的错误。如果传递了另一种类型的对象,则None等同于传递0,并且任何其他对象都被打印到stderr并导致退出代码为1。特别是,sys.exit(“某些错误消息”)是在发生错误时退出程序的一种快速方法。
所以,这是我修改后的答案:
import sys
try:
# ...
except Exception as error:
sys.exit(f'Failure in STA_1S ({error})')https://stackoverflow.com/questions/62552693
复制相似问题