Exception Handling
Exception Handling
In PL/SQL, a warning or error condition is called an exception. Exceptions can be internally defined (by the run-time system) or user defined. Examples of internally defined exceptions include division by zero and out of memory. Some common internal exceptions have predefined names, such as ZERO_DIVIDE and STORAGE_ERROR. The other internal exceptions can be given names.
When an error occurs, an exception is raised. That is, normal execution stops and control transfers to the exception-handling part of your PL/SQL block or subprogram. Internal exceptions are raised implicitly (automatically) by the run-time system. User-defined exceptions must be raised explicitly by RAISE statements, which can also raise predefined exceptions.
To handle raised exceptions, you write separate routines called exception handlers. After an exception handler runs, the current block stops executing and the enclosing block resumes with the next statement. If there is no enclosing block, control returns to the host environment
Example for handling a run time exception given below
Write a PL/SQL to find the factorial of a number entered by the user.Handle any error generated due to invalid data type entered by the user.
DECLARE
num NUMBER
answer NUMBER
BEGIN
num:=# --Here if the value entered by the user is not avalid data type, for eg: if it is --a character data then it will it raise run time error named value_error
ans:=1;
FOR i IN 1..num
LOOP ans:=ans*i;
END LOOP:
DBMS_OUTPUT.PUT_LINE('Fact is:'ans);
EXCEPTION
WHEN VALUE_ERROR
THEN DBMS_OUTPUT.PUT_LINE('The data entered id not valid');
END:

0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home