Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
83 views9 pages

Exception Handling

Exception handling in PL/SQL allows normal execution to stop and control to transfer to exception handling code when errors occur. There are two types of exceptions - predefined exceptions raised by the system for rule violations or limits, and user-defined exceptions explicitly raised using RAISE. When an exception is raised, control passes to exception handlers that use the WHEN clause to identify and handle specific exceptions.

Uploaded by

Ram Krishna
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views9 pages

Exception Handling

Exception handling in PL/SQL allows normal execution to stop and control to transfer to exception handling code when errors occur. There are two types of exceptions - predefined exceptions raised by the system for rule violations or limits, and user-defined exceptions explicitly raised using RAISE. When an exception is raised, control passes to exception handlers that use the WHEN clause to identify and handle specific exceptions.

Uploaded by

Ram Krishna
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 9

Exception handling

Introduction
• In PL/SQL ,a warning error condition is called an
exception
• When an error occurs , an exception is raised that is the
normal execution stops and the control transfers to the
exception handling part of the PL/SQL block.
• Exceptions of two types :
Predefined
User Defined
• Exceptions are identifiers in PL/SQL which may be
raised during the execution of a block to terminate its
main body of actions.
Predefined Exceptions:
• These are internally defined by the runtime system
• These are raised implicitly whenever program violates an
Oracle rule or exceeds a system dependent limit.
• Every Oracle error has a number, but exceptions must be
handled by a name.
• These are raised automatically.
Exception Handlers:
• Any type of exception is raised , control is passed to the
exception section.
• If the exception is not handled there ,the program terminates .
• We can handle the exception using following statement.
when <exception identifier> then actions
Declare
a emp%rowtype;
Begin
select * into a from emp where job=‘MANAGER’;
dbms_output.put_line(a.empno || a.ename);
Exception
when no_data_found then dbms_output.put_line (‘no
records in the table);
when too_many_rows then dbms_output.put_line(‘More
than one manager ‘);
End;
Error
• INVALID_CURSOR
• CURSOR_ALREADY_OPEN
• NO_DATA_FOUND
• TOO_MANY_ROWS
• LOGON_DENIED
• INVALID_NUMBER
• NO_DATA_FOUND
• VALUE_ERROR
• ZERO_DIVIDE
• OTHERS
• PL/SQL provides two functions for exception
handling:
SQLCODE : It returns the error number associated with
the exception. Outside the handler it returns zero.
SQLERRM : It returns character data .It returns the
complete error message.
Example:
when others then
dbms_output.put_line (‘error message’ || SQLERRM);
a := SQLCODE;
dbms_output.put_line (‘error message’ || a);
User defined

• These are declared and defined by the user.


• It must be raised explicitly using the RAISE statement.
• Declare
identifier EXCEPTION;
out_of_stock EXCEPTION;
Declarations are similar to variable. These cannot appear in
assignment or SQL statements.
Example:
declare
out_of_stock exception;
a number;
Begin
select qoh into a from itemmast where itno=1234;
if a < 10 then
raise out_of_stock;
end if;
exception
when out_of_stock then
dbms_output.put_line (‘no stock’);
when no_data_found then
dbms_output.put_line (‘no item exists);
end;
Scope rules
• Exceptions cannot be declared twice in the same
block.
• Exceptions declared in a block is local to that but
global to its subblock.
• Inner block exceptions can be propagated to
outer block if there is no handler.
• If the same exception is declared again in the sub
block, it overrides the global exception.

You might also like