Oracle PL/SQL - EXCEPTION Instead of ELSE Clause in CASE Statement

Introduction

The ELSE clause can be replaced by an EXCEPTION part.

Demo

SQL>
SQL> DECLARE-- ww  w . j av  a 2  s.co m
  2    final_mark CHAR(1);
  3  BEGIN
  4    final_mark := 'B';
  5
  6    CASE
  7      WHEN final_mark = 'A' THEN DBMS_OUTPUT.PUT_LINE('Excellent');
  8      WHEN final_mark = 'B' THEN DBMS_OUTPUT.PUT_LINE('Very Good');
  9      WHEN final_mark = 'C' THEN DBMS_OUTPUT.PUT_LINE('Good');
 10      WHEN final_mark = 'D' THEN DBMS_OUTPUT.PUT_LINE('Fair');
 11      WHEN final_mark = 'F' THEN DBMS_OUTPUT.PUT_LINE('Poor');
 12    END CASE;
 13  EXCEPTION
 14    WHEN CASE_NOT_FOUND THEN
 15      DBMS_OUTPUT.PUT_LINE('No such final_mark');
 16  END;
 17  /
Very Good

PL/SQL procedure successfully completed.

SQL>
SQL>

Related Topic