title_body: Plutus Pioneer Program - Lecture #4 Homework - Best practice for logging errors
upvoted_answer: The exception handler does not cover the first payment contract run. I've created a separate function for this that handles this error:
  payContract' :: Contract () PaySchema Void ()
  payContract' = Contract.handleError
      (\err -&gt; Contract.logError $ &quot;Caught error: &quot; ++ unpack err)
      payContract
  
  Then of course you have to call the payContract' in your emulator tracer as follows:
  h1 &lt;- activateContractWallet (Wallet 1) payContract'
  
  You can also combine everything together like this:
  payContract :: Contract () PaySchema Text ()
  payContract = Contract.handleError
      (\err -&gt; Contract.logError $ &quot;Caught error: &quot; ++ unpack err) $ do
      pp &lt;- endpoint @&quot;pay&quot;
      let tx = mustPayToPubKey (ppRecipient pp) $ lovelaceValueOf $ ppLovelace pp
      void $ submitTx tx
      payContract