0

I've a question about try-exception statements. It's NOT the same as the other question mentioned. As I do not need a finally statement...

I have for example the following code:

try:
    #dosomething
except TypeError:
    #dosomething -> THIS GIVES A TypeError!

"..."

    #I_want_this_to_be_executed_if_the_previous_commands_are_not_working

What can I fill in between the quotation marks to make sure this last command will be executed if the except TypeError doesn't work either? (it gives also an TypeError)

I do NOT need a finally statement as it is NOT necessary to be executed ALWAYS, only in case the exception also gives a TypeError...

Nelis
  • 91
  • 1
  • 11
  • For your last edit: an easier way is to put `dosomething` inside functions and do `for func in my_functions: try: func() except SomeException:pass`... I should say **never** use just `except`. Always specify which exception you want to catch, even if it is `Exception`. – Giacomo Alzetta Jul 31 '19 at 11:23
  • You can make things conditional in `finally` if the obvious -- nested `try` clauses -- is unacceptable for some reason. – tripleee Jul 31 '19 at 11:38
  • 1
    `try: ... except TypeError: try: ... except TypeError: "..."`…?! – deceze Jul 31 '19 at 11:43
  • 1
    Another `try..except` inside the `except` clause would be the obvious answer. If you show us more details about what you're trying to do, we may be able to suggest a more succinct alternative. – deceze Jul 31 '19 at 11:46
  • @deceze This is what I needed! Thank you very much! I am as you can propably see a beginner... – Nelis Jul 31 '19 at 11:50

1 Answers1

0

Just add: (EDIT: didn't understand the question, the comments are correct)

try:
    #dosomething
except TypeError:
    try:
        #What you were going to do
    except TypeError:
        #Handle the second typeError
Jonas
  • 159
  • 8