Published on

Published in: Education, Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. Exception<br />An exception is a runtime error or warning condition, which can be predefined or user-defined. Predefined exceptions are raised implicitly (automatically) by the runtime system. User-defined exceptions must be raised explicitly by RAISE statements. To handle raised exceptions, you write separate routines called exception handlers.<br />
  2. 2. Types of exception<br />There are two types of exception<br />Predefined exception<br />User defined exception<br />
  3. 3. Predefined exception<br />Predefined exceptions are the ones which are already defined by Oracle team that correspond to the most common oracle errors (like division by zero, out of memory, etc.). <br />We can handle them directly within our program without declaring them, and we have thousands of such type of exceptions. In this article, I cover the most important ones.<br />
  4. 4.
  5. 5. syntax<br />Begin<br />Sequence of statements;<br />Exception<br />When exception name1 then sequence of statements;<br />When exception name2 then sequence of statements;<br />End;<br />
  6. 6.
  7. 7. Data are not found<br />set serveroutput on<br />declare<br />y wel.sub1%type;<br />begin<br />select sub1 into y from wel where name=&apos;mathu&apos;;<br />exception<br />when no_data_found then<br />dbms_output.put_line(&apos;datas are not found&apos;);<br />end;<br />Output:<br /> PL/SQL procedure successfully completed.<br />
  8. 8. set serveroutput on<br />declare<br />y wel.sub1%type;<br />begin<br />select sub1 into y from wel where name=&apos;muthu&apos;;<br />exception<br />when no_data_found then<br />dbms_output.put_line(&apos;datas are not found&apos;);<br />end;<br />Output:<br />datas are not found PL/SQL procedure successfully completed.<br />
  9. 9. Too_many_rows<br />set serveroutput on<br />declare<br />y wel.sub1%type;<br />begin<br />select sub1 into y from wel where sub2=99;<br />exception<br />when too_many_rows then<br />dbms_output.put_line(&apos;Two many rows&apos;);<br />end;<br />Output:<br />Two many rows PL/SQL procedure successfully completed.<br />
  10. 10. User Defined Exception<br />User-Defined Exceptions must be declared and raised explicitly by the user (by issuing RAISE statements). These exceptions are created, used, raised and implemented by user himself. <br />Oracle will not know about any of those exceptions (till it finds the declarations of those exceptions within the PL/SQL block).<br /> There exists EXCEPTION section within the PL/SQL block to handle any sort of exceptions and the section is optional. Let us start by looking into some of the most commonly used predefined exceptions.<br />
  11. 11. Raise statement<br />raise_application_error(      error_number, message[, {TRUE | FALSE}]);<br />error_number it is a negative integer in the range -20000 .. -20999<br />message is a character string up to 2048 bytes long.<br />If the optional third parameter is TRUE, the error is placed on the stack of previous errors.<br /> If the parameter is FALSE (the default), the error replaces all previous errors. <br />RAISE_APPLICATION_ERROR is part of package DBMS_STANDARD, and as with package STANDARD, you do not need to qualify references to it.<br />
  12. 12. set serveroutput on<br />prompt 1.addition<br />prompt 2.subtraction<br />prompt 3.multplication<br />prompt 4.division<br />accept n number prompt &apos;Enter your choice:&apos;<br />declare<br /> d number := &n;<br /> a number := &a;<br /> b number := &b;<br /> c number(5);<br />add_excep exception;<br />sub_excep exception;<br />mul_excep exception;<br />div_excep exception;<br />
  13. 13. begin<br />if d=1 then<br />if(a&gt;0 and b&gt;0)then<br />dbms_output.put_line(&apos;ADDTION:’ ||c);<br />else<br />raisadd_excep;<br />end if;<br />end if;<br />
  14. 14. if d=2 then<br />if(a&gt;0 and b&gt;0) then<br />c:=a-b;<br />dbms_output.put_line(&apos;subtraction:&apos;||c);<br />else<br />raise sub_excep;<br />end if;<br />end if;<br />
  15. 15. if d=3 then <br />if(a&gt;0 and b&gt;0) then<br />c:=a*b;<br />dbms_output.put_line(&apos;multiplication:&apos;||c);<br />else<br />raise mul_excep;<br />end if;<br />end if;<br />
  16. 16. if d=4 then<br />if(a&gt;0 and b&gt;0)then<br />c:=a/b;<br />dbms_output.put_line(&apos;division:&apos;||c);<br />else<br />raise div_excep;<br />end if;<br />end if;<br />
  17. 17. exception <br />when add_excep then<br />dbms_output.put_line(&apos;arithmetic exception&apos;);<br /> <br />when sub_excep then<br />dbms_output.put_line(&quot;enter passitive values&apos;);<br /> <br />when mul_excep then<br />dbms_output.put_line(&apos;enter valid input&apos;);<br /> <br />when div_excep then <br />dbms_output.put_line(&apos;divided by zero exception&apos;);<br />end;<br /> <br />
  18. 18. Output<br />1.addition 2.subtraction 3.multplication 4.division old 2: d number := &n; new 2: d number := 3; old 3: a number := &a; new 3: a number := 10; old 4: b number := &b; new 4: b number := 23; <br />multiplication: 230PL/SQL procedure successfully completed.<br />
  19. 19. Department of CE/ITMSPVL Polytechnic collegePavoorchatram<br />