4

If I have a try except block in my python code, and the first line of my try statement raises an exception will it automatically move on to exception or will it finish the try block first?

try:
    int(string)
    string = "This was a mistake, can't int string"
except:
    pass

Here is it checking if it can int(string), which it can't, and then it immediately moves onto except, or does it do the string assignment first?

When I run it, it seems like it stops right away, but I want to know if that's happening for sure or something else.

Thanks

chung
  • 835
  • 1
  • 7
  • 19
  • does this answer your question. https://stackoverflow.com/questions/19522990/python-catch-exception-and-continue-try-block – Vishal Singh Jun 26 '20 at 03:20
  • See this, https://stackoverflow.com/a/19593202/4985099 – sushanth Jun 26 '20 at 03:20
  • 1
    You've got the right idea. It goes to the except block immediately when it hits an exception, and it doesn't run the rest of the code. But you already know that! You ran it, you saw what it does. Trust the code. That's how it works and you don't need someone on the internet to confirm that when you have already done so! – Alterlife Jun 26 '20 at 03:21
  • 2
    That's true, sorry, I'm just building a my first project with python and don't want to mess up! – chung Jun 26 '20 at 03:24

5 Answers5

8

Let's try it.

try:
    1/0
    print("what?")
except:
    print("nope")

Output:

nope
>>> 

When an exception is raised it moves on to the except.

Starbuck5
  • 1,649
  • 2
  • 7
  • 13
2

Hope it helps!

#the try block is to run the process
    try:
        string = "This was a mistake, can't int string"
        #you are parsing the string into an int 
        int(string)
    
    #this block is to capture the exception when it is raised
    except:
        #here I am just printing the exception 
        print('Exception')
        #the pass is to pass the execution and follow the remaining process.
        pass

Output:

Exception
>>>
thisisjaymehta
  • 614
  • 1
  • 12
  • 26
Ricky
  • 2,662
  • 5
  • 25
  • 57
2

From the Python Errors and Exceptions tutorial documentation, I found this:

The try statement works as follows.

  • First, the try clause (the statement(s) between the try and except keywords) is executed.
  • If no exception occurs, the except clause is skipped and execution of the try statement is finished.
  • If an exception occurs during execution of the try clause, the rest of the clause is skipped. Then if its type matches the exception named after the except keyword, the except clause is executed, and then execution continues after the try statement.
  • If an exception occurs which does not match the exception named in the except clause, it is passed on to outer try statements; if no handler is found, it is an unhandled exception and execution stops with a message as shown above.
Kyle F Hartzenberg
  • 2,567
  • 3
  • 6
  • 24
mettleap
  • 1,390
  • 8
  • 17
1

Well, that the whole point of an error handler. When an error occurs in out program, it crashes, it won't simply skip the error line.

try:
    int(string)
    string = "This was a mistake, can't int string"
except:
    pass

So if the try block finishes even after an error occurred in the middle, then the error handler'd seem a bit pointless.

Red
  • 26,798
  • 7
  • 36
  • 58
-1

There seem to be some issue with your program and maybe your understanding of the try except block.

First of all, from the block of code you have provided, it seems to me like string isn't even defined yet when you try to pass int(string).

Secondly, the string variable which you seem to be defining under seems to me like is something you are trying to print to the console once the program spits out an error and passes to the except block. If this is what you are trying to do, then your code should look something like this:

try:
    int(string)
except:
    print("This was a mistake, can't int string")

If you were to run the code provided above, you would indeed confirm that the try except statement is working as it would print "This was a mistake, can't int string" into the console instead of giving an error. Hope this was somewhat helpful and understandable. You got this chief, keep grinding!