title: Python try-except with of if else
body: <p>I have the following code:</p>
  
  <pre><code>    try:
          pk = a_method_that_may_raise_an_exception()
      except:
          method_to_be_executed_in_case_of_exception_or_pk_is_false()
      else:
          if pk:
              process_pk()
          else:
              method_to_be_executed_in_case_of_exception_or_pk_is_false()
  </code></pre>
  
  <p>This could be written as:</p>
  
  <pre><code>    try:
          if a_method_that_may_raise_an_exception():
              process_pk()
          else:
              method_to_be_executed_in_case_of_exception_or_pk_is_false()
      except:
          method_to_be_executed_in_case_of_exception_or_pk_is_false()
  </code></pre>
  
  <p>I am not happy that the method <code>method_to_be_executed_in_case_of_exception_or_pk_is_false()</code>
  appears twice, i.e in else of both if and try...except.</p>
  
  <p>Is there a better way to do this?</p>