Oracle SQL Developer
Oracle SQL Developer
Oracle SQL Developer
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Handling PL/<strong>SQL</strong> ErrorsExceptionTOO_MANY_ROWSVALUE_ERRORZERO_DIVIDETable 7–1(Cont.) Predefined PL/<strong>SQL</strong> ExceptionsDescriptionA SELECT INTO statement returns more than one row.An arithmetic, conversion, truncation, or size-constraint error occurs. Forexample, when your program selects a column value into a character variable, ifthe value is longer than the declared length of the variable, PL/<strong>SQL</strong> aborts theassignment and raises VALUE_ERROR. In procedural statements, VALUE_ERRORis raised if the conversion of a character string into a number fails. (In <strong>SQL</strong>statements, INVALID_NUMBER is raised.)A program attempts to divide a number by zero.7.4.2 Using the Exception HandlerUsing exceptions for error handling has several advantages. With exceptions, you canreliably handle potential errors from many statements with a single exception handler:Example 7–29Managing Multiple Errors With a Single Exception HandlerDECLARE -- declare variablesemp_column VARCHAR2(30) := 'last_name';table_name VARCHAR2(30) := 'emp'; -- set value to raise errortemp_varVARCHAR2(30);BEGINtemp_var := emp_column;SELECT COLUMN_NAME INTO temp_var FROM USER_TAB_COLSWHERE TABLE_NAME = 'EMPLOYEES' AND COLUMN_NAME = UPPER(emp_column);-- processing heretemp_var := table_name;SELECT OBJECT_NAME INTO temp_var FROM USER_OBJECTSWHERE OBJECT_NAME = UPPER(table_name) AND OBJECT_TYPE = 'TABLE';-- processing hereEXCEPTIONWHEN NO_DATA_FOUND THEN -- catches all 'no data found' errorsDBMS_OUTPUT.PUT_LINE ('No Data found for SELECT on ' || temp_var);END;/7.4.3 Declaring PL/<strong>SQL</strong> ExceptionsExceptions can be declared only in the declarative part of a PL/<strong>SQL</strong> block,subprogram, or package. You declare an exception by introducing its name, followedby the keyword EXCEPTION. In Example 7–30, you declare an exception namedpast_due that is raised when the due_date is less than the today's date.Exception and variable declarations are similar. But remember, an exception is an errorcondition, not a data item. Unlike variables, exceptions cannot appear in assignmentstatements or <strong>SQL</strong> statements. However, the same scope rules apply to variables andexceptions.7.4.4 Scope Rules for PL/<strong>SQL</strong> ExceptionsYou cannot declare an exception twice in the same block. You can, however, declare thesame exception in two different blocks.Exceptions declared in a block are considered local to that block and global to all itssub-blocks. Because a block can reference only local or global exceptions, enclosingblocks cannot reference exceptions declared in a sub-block.PL/<strong>SQL</strong>: Usage Information 7-23