title: Handling EXCEPTION and return result from function
body: <p>This is my code</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION test_excep (arg INTEGER) RETURNS INTEGER 
  AS $$
      DECLARE res INTEGER;
      BEGIN
          res := 100 / arg;
  
          BEGIN
              EXCEPTION
              WHEN division_by_zero 
              THEN  RETURN 999;
          END;
  
  
          RETURN res;
      END;
  
  
  
  $$
  LANGUAGE plpgsql;
  </code></pre>
  
  <p>That is, I need returned "999", if happened division by zero, but this: <code>SELECT test_excep(0)</code></p>
  
  <p>returns error:  <code>division by zero
  CONTEXT:  PL/pgSQL function test_excep(integer) line 4 at assignment</code></p>
  
  <p>What is wrong in my code?</p>