Python Forum
is try/except safe inside of another except?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
is try/except safe inside of another except?
#1
is it safe to have a try/except within the except of another try/except?

i want to get some data that may fail, resulting in an exception. then there is an alternate place to get it and that, too, may throw an exception. then there is even a 3rd place that may throw an exception. if any work, tried in that specific order, of course i want to return the data. else i want to return n*(None,).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Maybe I something misunderstand, but you can iterate over data locations, e.g. something like this:

def func():
    data = None
    for place in places:
        try:
            data = get_data_from_place(place)
        except CustomException: # or exceptions
            pass

    return data
Reply
#3
i mean something like:
    try:
        ... something that could raise an exception
    except the_exception_to_deal_with:
        try:
            ... the handler that could raise a different exception
        except the_different_exception:
            ... the handler for the 2nd exception
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
compare
try: # first source
    ... something that could raise an exception
except the_exception_to_deal_with:
    try: # second source
        ... the handler that could raise a different exception
    except the_different_exception:
        try: # third source
        ... the handler that could raise a different exception
        except the_different_exception:
            ... the handler for the 3rd exception
with what scidam suggested (I just added the break)
def get_data(places):
    data = None
    for place in places:
        try:
            data = get_data_from_place(place)
            break # will ne executed if no exception, i.e. respective place returned data
        except SomeException: # or exceptions
            pass
    return data
it's more readable and you can have any number of places to look at without any additional code/extra levels
you may pair place and exception if by chance different places raise different exception
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
with break, now that makes more sense.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
if you prefer without break
def get_data(places):
    for place in places:
        try:
            data = get_data_from_place(place)
        except Exception: # use specific exception or exceptions
            continue
        else:
            return data
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  When is it safe to compare (==) two floats? Radical 4 709 Nov-12-2023, 11:53 AM
Last Post: PyDan
  is it safe to close stdin Skaperen 1 2,666 Apr-04-2019, 06:57 AM
Last Post: Gribouillis
  how this to make a asyncio thread safe? linziyan 0 2,353 Jun-07-2018, 10:33 AM
Last Post: linziyan
  Is it safe to use outside a class created inside the function? oneroslavsky 2 2,964 Dec-27-2017, 01:10 AM
Last Post: mpd

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020