try statements do not create a new scope, but text won't be set if the call to url lib.request.urlopen raises the exception. You probably want the print(text) line in an else clause, so that it is only executed when there is no exception.

try:
    url = "http://www.google.com"
    page = urllib.request.urlopen(url)
    text = page.read().decode('utf8')
except (ValueError, RuntimeError, TypeError, NameError):
    print("Unable to process your request dude!!")
else:
    print(text)

If text needs to be used later, you really need to think about what its value is supposed to be if the assignment to page fails and you can't call page.read(). You can give it an initial value prior to the try statement:

text = 'something'
try:
    url = "http://www.google.com"
    page = urllib.request.urlopen(url)
    text = page.read().decode('utf8')
except (ValueError, RuntimeError, TypeError, NameError):
    print("Unable to process your request dude!!")

print(text)

or in the else clause:

try:
    url = "http://www.google.com"
    page = urllib.request.urlopen(url)
    text = page.read().decode('utf8')
except (ValueError, RuntimeError, TypeError, NameError):
    print("Unable to process your request dude!!")
else:
    text = 'something'

print(text)
Answer from chepner on Stack Overflow
๐ŸŒ
Python
bugs.python.org โ€บ issue43458
Issue 43458: Tutorial should mention about variable scope in try/except/finally - Python tracker
March 10, 2021 - This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide ยท This issue has been migrated to GitHub: https://github.com/python/cpython/issues/87624
๐ŸŒ
DEV Community
dev.to โ€บ andrewchaa โ€บ python-try-blocks-a-breath-of-fresh-air-for-variable-scope-unlike-java-c-1k6l
Python `try` Blocks: A Breath of Fresh Air for Variable Scope (Unlike Java & C#!) - DEV Community
March 10, 2025 - Let's see how Python's approach shines in practice: try: value = "Hello, World!" # Variable defined inside try except Exception as e: print(f"An error occurred: {e}") # `value` is immediately accessible here!
Top answer
1 of 3
145

try statements do not create a new scope, but text won't be set if the call to url lib.request.urlopen raises the exception. You probably want the print(text) line in an else clause, so that it is only executed when there is no exception.

try:
    url = "http://www.google.com"
    page = urllib.request.urlopen(url)
    text = page.read().decode('utf8')
except (ValueError, RuntimeError, TypeError, NameError):
    print("Unable to process your request dude!!")
else:
    print(text)

If text needs to be used later, you really need to think about what its value is supposed to be if the assignment to page fails and you can't call page.read(). You can give it an initial value prior to the try statement:

text = 'something'
try:
    url = "http://www.google.com"
    page = urllib.request.urlopen(url)
    text = page.read().decode('utf8')
except (ValueError, RuntimeError, TypeError, NameError):
    print("Unable to process your request dude!!")

print(text)

or in the else clause:

try:
    url = "http://www.google.com"
    page = urllib.request.urlopen(url)
    text = page.read().decode('utf8')
except (ValueError, RuntimeError, TypeError, NameError):
    print("Unable to process your request dude!!")
else:
    text = 'something'

print(text)
2 of 3
10

As answered before there is no new scope introduced by using try except clause, so if no exception occurs you should see your variable in locals list and it should be accessible in current (in your case global) scope.

print(locals())

In module scope (your case) locals() == globals()

๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ assign a global variable in try/except
r/learnpython on Reddit: assign a global variable in try/except
September 7, 2017 -

Hej guys. I have a problem assign a global variable inside a try/except. The try/except is in the main function and iam using the variable in another function. when i print the variable outside the main function i get "something" instead of the assigned value. how can i solve it?

company ="something"  #global variable

               try:
                    loggInThisFile = open(reportFilePath,'w')
                except Exception as e:
                    print("can't create report file:", reportFilePath)
                else:
                    company = link.split(" ; ")[1]
                    print(company)     #prints assigned value 

ps: the declaration and assignation is done before it soposed to be used.
Thanks in advance

๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ help: can you create a variable that is a try/exception block?
r/learnpython on Reddit: Help: Can you create a variable that is a try/exception block?
January 9, 2022 -

I have some code that pulls stock information into an excel sheet using the tickers present in the excel file, and I recently am trying to expand this code to pull in the stocks sectors using yfinance. The issue is that some of my tickers are EFT's, which do not have sectors. So whenever my code tries to run, it will throw a syntax error when it reaches an EFT stock. If I take out all the EFT's, it runs just fine.

What I'm trying to do is set my variable (sector) as a try/exception block, but I don't know if this is possible. Could someone please help me out on this? Examples or suggestions? I'm not exactly new to Python, but I'm not a regular program either. Just really lost and in hard core struggle mode on this one.

Excerpt from my code below, along with what I'm having trouble with developing below that.

import xlwings as xw
from yahoofinancials import YahooFinancials
import pandas as pd
import yfinance as yf

def main():
    wb = xw.Book.caller()
    sheet = wb.sheets[0]
    df = pd.DataFrame()
    tickers = sheet.range("D7").options(expand='down').value
    for ticker in tickers:
        find_sector = yf.Ticker(ticker)
        sector = find_sector.info['sector']

        new_row = {
            "GICS Sector": sector,
        }
        df = df.append(new_row, ignore_index=True)
    sheet.range("E6").options(index=False).value = df

if __name__ == "__main__":
    xw.Book("dividends.xlsm").set_mock_caller()
    main()

What I'm envisioning/trying/struggling with for setting my 'sector' variable:

sector =
    try:
        find_sector.info['sector']
    else:
        print('EFT')
Find elsewhere
๐ŸŒ
GitHub
github.com โ€บ python โ€บ cpython โ€บ issues โ€บ 87624
Tutorial should mention about variable scope in try/except/finally ยท Issue #87624 ยท python/cpython
March 10, 2021 - assignee = None closed_at = None created_at = <Date 2021-03-10.05:08:31.470> labels = ['type-feature', '3.9', 'docs'] title = 'Tutorial should mention about variable scope in try/except/finally' updated_at = <Date 2021-03-12.19:47:11.330> user = 'https://bugs.python.org/deekox' bugs.python.org fields: activity = <Date 2021-03-12.19:47:11.330> actor = 'eric.araujo' assignee = 'docs@python' closed = False closed_date = None closer = None components = ['Documentation'] creation = <Date 2021-03-10.05:08:31.470> creator = 'deekox' dependencies = [] files = [] hgrepos = [] issue_num = 43458 keywords
Author: python
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ does this try/except behaviour make any sense?
r/learnpython on Reddit: Does this try/except behaviour make any sense?
February 16, 2023 -

Dear Pythonistas, I am getting more experience using try/except but I have a situation where Python appears to behave weirdly IMO.

My (a bit complex) code where I work with connection errors can be reduced to an equivalent small example:

try:
    1 / 0
except ZeroDivisionError as error:
    try:
       [1, 2, 3][3]
    except IndexError as error:
        pass
    print("The last error was:", error)

Wouldn't you also expect it to print the index error? It doesn't! Instead, a NameError for the variable error is thrown. Does anybody understand why?

I have a few more details on a Stack Overflow post where you are welcome to answer if you are looking for reputation in there:

https://stackoverflow.com/questions/75473452/why-is-python-throwing-a-nameerror-when-doing-nested-exceptions-with-same-variab

๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ scope 'of except exception as e'
r/learnpython on Reddit: Scope 'of except Exception as e'
November 20, 2017 - Does e remain accessible in the 2nd print statement, or is it just localised within the try-except scope? try: foo() except Exception as e: print('Inside try-except:',e) print('Outside try-except:',e) Share ...
๐ŸŒ
Real Python
realpython.com โ€บ python-scope-legb-rule
Python Scope and the LEGB Rule: Resolving Names in Your Code โ€“ Real Python
July 16, 2026 - Here, the loop variable holds the last value processed by the loop, which is 4 in this example. Another atypical scope is related to the optional variable in an except clause. The exception variable is a variable that holds a reference to any exception raised in a try statement.
๐ŸŒ
Julia Programming Language
discourse.julialang.org โ€บ general usage
Confusion about scope in try-catch blocks - General Usage - Julia Programming Language
June 11, 2024 - Regarding scoping rules this used to trip me up a bit: Python: >>> try: ... x = 2 ... except: ... pass ... >>> x 2 Julia: julia> try x = 2 catch end 2 julia> x ERROR: UndefVarError:โ€ฆ