嵌套try中的Python引发异常

嵌套try中的Python引发异常,python,exception,exception-handling,try-catch,Python,Exception,Exception Handling,Try Catch,我在try中有一个嵌套的try/except块,我想引发错误,然后中断该方法,但它仍在继续 try: #stuff try: #do some stuff except: raise CustomException('debug info') #do some more stuff except CustomException: #stuff #do even more stuff return stuff 当前,在它引发

我在try中有一个嵌套的try/except块,我想引发错误,然后中断该方法,但它仍在继续

try:
    #stuff
    try:
        #do some stuff
    except:
        raise CustomException('debug info')
    #do some more stuff
except CustomException:
    #stuff
#do even more stuff
return stuff

当前,在它引发CustomException(第5行)之后,它会跳到Exception(第7行),然后继续并最终返回。我希望它在上升时折断,但不要被水抓住。如果CustomException发生在“#多做一些事情”中,它仍然需要捕获它并继续执行。

如何更改try的结构

try:
    #do some stuff
except:
    raise CustomException('debug info')
try:
    #do some more stuff
except CustomException:
    #stuff
#do even more stuff
return stuff

除了以下几点外,try的结构如何改变

try:
    #do some stuff
except:
    raise CustomException('debug info')
try:
    #do some more stuff
except CustomException:
    #stuff
#do even more stuff
return stuff

除了以下几点外,try的结构如何改变

try:
    #do some stuff
except:
    raise CustomException('debug info')
try:
    #do some more stuff
except CustomException:
    #stuff
#do even more stuff
return stuff

除了以下几点外,try的结构如何改变

try:
    #do some stuff
except:
    raise CustomException('debug info')
try:
    #do some more stuff
except CustomException:
    #stuff
#do even more stuff
return stuff

只需编写
raise
,即可重新引发捕获的异常

try:
    #stuff
    try:
        #do some stuff
    except:
        raise CustomException('debug info')
    #do some more stuff
except CustomException:
    #stuff
    raise  ## KM: This re-raise the exception

## KM: This wont be executed if CustomException('debug info') was raised
#do even more stuff

return stuff

只需编写
raise
,即可重新引发捕获的异常

try:
    #stuff
    try:
        #do some stuff
    except:
        raise CustomException('debug info')
    #do some more stuff
except CustomException:
    #stuff
    raise  ## KM: This re-raise the exception

## KM: This wont be executed if CustomException('debug info') was raised
#do even more stuff

return stuff

只需编写
raise
,即可重新引发捕获的异常

try:
    #stuff
    try:
        #do some stuff
    except:
        raise CustomException('debug info')
    #do some more stuff
except CustomException:
    #stuff
    raise  ## KM: This re-raise the exception

## KM: This wont be executed if CustomException('debug info') was raised
#do even more stuff

return stuff

只需编写
raise
,即可重新引发捕获的异常

try:
    #stuff
    try:
        #do some stuff
    except:
        raise CustomException('debug info')
    #do some more stuff
except CustomException:
    #stuff
    raise  ## KM: This re-raise the exception

## KM: This wont be executed if CustomException('debug info') was raised
#do even more stuff

return stuff

您基本上是在引发异常并立即捕获它。这毫无意义,当然会继续下去。如果你想“脱离方法”,你必须让异常离开你的方法,而不是捕获它。使用不同的异常类怎么样?你基本上是在引发异常并立即捕获它。这毫无意义,当然会继续下去。如果你想“脱离方法”,你必须让异常离开你的方法,而不是捕获它。使用不同的异常类怎么样?你基本上是在引发异常并立即捕获它。这毫无意义,当然会继续下去。如果你想“脱离方法”,你必须让异常离开你的方法,而不是捕获它。使用不同的异常类怎么样?你基本上是在引发异常并立即捕获它。这毫无意义,当然会继续下去。如果你想“打破方法”,你必须让异常离开你的方法,而不是捕获它。使用不同的异常类怎么样?哎呀,忘了添加一个#dostuff@SirDeimos,我不知道,除了3个试块,做一些事情,再做一些事情。哎呀,忘了添加一个#dostuff@SirDeimos,我不知道,除了3个试块,做一些事情,再做一些事情。哎呀,忘了添加一个#dostuff@SirDeimos,我不知道,除了3个试块,做一些事情,再做一些事情。哎呀,忘了添加一个#dostuff@SirDeimos,我不知道,除了3个试块,做一些东西,多做些事情。