title: Elegant way of handling PostgreSQL exceptions?
body: <p>In PostgreSQL, I would like to create a safe-wrapping mechanism which returns empty result if an exception occurs. Consider the following:</p>
  
  <pre><code>SELECT * FROM myschema.mytable;
  </code></pre>
  
  <p>I could do the safe-wrapping in the client application:</p>
  
  <pre><code>try {
      result = execute_query('SELECT value FROM myschema.mytable').fetchall();
  }
  catch(pg_exception) {
      result = []
  }
  </code></pre>
  
  <p>But could I do such a thing in SQL directly? I would like to make the following code work, but it seems like it should by put into <code>DO $$ ... $$</code> block and here I'm getting lost.</p>
  
  <pre><code>BEGIN
      SELECT * FROM myschema.mytable;
  EXCEPTION WHEN others THEN
      SELECT unnest(ARRAY[]::TEXT[])
  END
  </code></pre>