truease.com
truease.com

Reputation: 1301

Can python ignore the error and return to the line next to the raised line and going on

Something just like that

try: 
    1/0
    print "hello world"
    print "every thing seems fine..."

except ZeroDivisionError: 
    print "It is not a critical error, go to next..."
    SomeWayAllowMeToExeutePrintHelloWorld_TheLineNextToTheRaisedLine()

except: 
    print "I have no idea, just stop work..."

After the [1/0] raised, and [except ZeroDivisionError] catched the error, and then return to the [print "hello world"] line, and going on...

Upvotes: 0

Views: 252

Answers (6)

jorispilot
jorispilot

Reputation: 483

You can put the print lines after the try... except statement, and use a second try... except statement for the second except.

try:
    try: 
        1/0

    except ZeroDivisionError: 
        print "It is not a critical error, go to next..."

except: 
    print "I have no idea, just stop work..."

print "hello world"
print "every thing seems fine..."

Althrough, in the second except case, if you just want to stop the program you should not catch the exception.

Upvotes: 1

Inbar Rose
Inbar Rose

Reputation: 43467

The correct usage of the try/except clause here is to use else or finally or both, as other answers have shown.

But, you can also refactor your code so each step of the attempt is its own function, and then put it all together nicely using a loop.

Example:

steps = [step1, step2, step3] # pointers to function/methods

for step in steps:
    try:
        step()
    except Exception as exc:
        # handle the exception

On your code:

def step1():
    1/0

def step2():
    print "hello world"

def step3():
    print "every thing seems fine..."

steps = [step1, step2, step3]

for step in steps:
    try:
        step()
    except ZeroDivisionError:
        print "It is not a critical error, go to next..."
    except:
        print "I have no idea, just stop work..."

The result:

>>> 
It is not a critical error, go to next...
hello world
every thing seems fine...

Upvotes: 0

Nicola Musatti
Nicola Musatti

Reputation: 18228

No. The way to achieve what you want is to use two try statements:

try:
    try: 
        1/0
    except ZeroDivisionError: 
        print "It is not a critical error, go to next..."

    print "hello world"
    print "every thing seems fine..."
except: 
    print "I have no idea, just stop work..."

Upvotes: 2

TerryA
TerryA

Reputation: 59984

You can't without specifically mentioning where you want to catch the error and what doesn't need to be caught.

You can add a finally: code block after doing the above:

try:
    1/0
except ZeroDivisionError:
    print "error"
finally:
    print "hello world"
    print "every thing seems fine..."

Upvotes: 1

Eric
Eric

Reputation: 97631

You can't, and there's no reason you should want to:

try: 
    1/0
except ZeroDivisionError:
    print "It is not a critical error, go to next..."

print "hello world"
print "every thing seems fine..."

Consider this code:

try: 
    important_res = f(1/0)
    send_important_message(important_res)
except ZeroDivisionError: 
    print "It is not a critical error, go to next..."
    SomeWayAllowMeToExeutePrintHelloWorld_TheLineNextToTheRaisedLine()

If you allow execution to resume, how do you pick a value to pass to f?

Upvotes: 4

Hrishi
Hrishi

Reputation: 7138

No. When an exception is raised, it always goes to the block which catches it. If you want to return to the line after the one which caused the exception, you should handle the exception immediately and then have that line below the code that handles the exception.

Upvotes: 3

Related Questions