title: Generic method to catch error return using expect script (shell)
body: <p>I have a expect script which interacts with modem on specific IP and port number. I want to report error to the user if spawn telnet   timeouts or not able to connect for some reason. In short, I want to print a error message and exit if the command fails rather than the script continuing further.</p>
  
  <pre><code>set -e doesn't work on expect script.
  </code></pre>
  
  <p>Your advise will help me fix this issue. thanks.</p>
  
  <p>Would like to re-emphasis that I am using expect script and not bash/sh. </p>
  
  <pre><code>#!/usr/bin/expect
  try {
      spawn telnet $HOSTIP $HOSTPORT
  } on error {
      exit 1
  }
  ....
  ....
  </code></pre>