SlideShare ist ein Scribd-Unternehmen logo
1 von 36
Downloaden Sie, um offline zu lesen
Database Lab
Swapnali Pawar
Predefined & User Defined
Exceptions
Swapnali Pawar
• An exception is an error condition during a program
execution.An error or a warning event is called an exception.
• Any abnormal condition or say event that interrupts the
normal flow of your program’s instructions at run time is an
exception. Or in simple words you can say an exception is a
run time error.
• Exceptions are designed for run time error handling rather
than compile time error handling. Errors that occur during
compilation phase are detected by the PL/SQL compiler and
reported back to the user.
What is an Exception?
Swapnali Pawar
Exceptions
Swapnali Pawar
Swapnali Pawar
Exception Handling
There are two types of PL/SQL exceptions in Oracle
database.
1. PreDefined / System-defined
Exceptions
2. User-Defined Exceptions
Types of Exceptions
Swapnali Pawar
• System-defined exceptions are defined and maintained
implicitly by the Oracle server.These exceptions are mainly
defined in the Oracle STANDARD package.Whenever an
exception occurs inside the program.The Oracle server
matches and identifies the appropriate exception from the
available set of exceptions.
• System defined exceptions majorly have a negative error code
and error message.These errors have a short name which is
used with the exception handler.
1. System-Defined Exceptions
Swapnali Pawar
Unlike System-Define Exception, User-Define Exceptions are
raised explicitly in the body of the PL/SQL block i.e more
specifically inside the BEGIN-END section)using the RAISE
Statement.
2. User-Define Exceptions
Swapnali Pawar
• If our code does not have an exception handling, then each
time we execute a statement, we must verify errors in
execution
.
• If we avoid exception handling in our code, the actual errors
get missed which gives rise to some other errors.
• The exception handling allows skipping multiple verifications
in the code.
• It provides better readability of the code by isolating the error
handlers in the code
Advantages Of Exception Handling
Swapnali Pawar
1. Pre-Defined Exception
• system defined exceptions are thrown by
default.
• Some of the popular System defined exceptions
are out of memory and division by
zero, having names like STORAGE_ERROR and
ZERO_DIVIDE respectively.
Swapnali Pawar
Exception
Oracle
Error
SQL
CODE
Description
ACCESS_INTO_NULL ORA - 06530 -6530 This exception is raised if a null object is naturally assigned a
value.
CASE_NOT_FOUND ORA - 06592 -6592 This exception is raised if none of the options in theWHEN
clause is chosen and there is no existence of an ELSE clause.
COLLECTION_IS_NULL ORA - 06531 -6531 This exception is raised when the code tries to apply collection
methods except EXISTS to a nested table or varray which is not
initialized. It can also be raised if our code tries to assign values
to a nested table or varray which is not initialized.
DUP_VAL_ON_INDEX ORA - 00001 -1 This exception is raised if duplicate values are tried to be stored
in a column that is constrained by a unique index.
CURSOR_ALREADY_OP
EN
ORA - 06511 -6511 This exception is raised if our code tries to open an already open
cursor.
INVALID_CURSOR ORA - 01001 -1001 This exception is raised if we try to do some operations on
cursors which are not permitted.For example, attempting to
close an already closed cursor.
INVALID_NUMBER ORA - 01722 -1722 This exception is raised if the conversion to a character string to
a number does not pass as the string is representing an invalid
number.
Pre-Defined Exception
Swapnali Pawar
Exception
Oracle
Error
SQL
CODE
Description
SYS_INVALID_ROWID ORA-01410 -1410 This exception is raised if the
conversion to a character string to a
universal row id does not pass as the
character string is representing an
invalid row id.
TIMEOUT_ON_RESOUR
CE
ORA-00051 -51 This exception is raised if Oracle is
waiting for a resource.
VALUE_ERROR ORA-06502 -6502 This exception is raised if a
mathematical, conversion, truncation
error happens in our program.
ZERO_DIVIDE ORA-01476 -1476 This exception is raised if we try to
divide a number by 0.
Swapnali Pawar
1. Pre-Defined Exception
Exceptions Defined by Oracle i.e System Defined
Exceptions-
1 . Zero_divide
2 . value_error
3 . Invalid_Number
4 . N0_Data_Found
5 . Too_Many_Rows
6 . Dup_Val_On_Index
7 . Cursor_Already_Open
8 . Invalid_Cursor
Swapnali Pawar
This Exception occurs if any number is divided by
Zero .
Eg-
a := &a; # 16
b := &b; # 0
c := a/b; # Occurs Divide_Zero
Exception
1. Zero_divide
Swapnali Pawar
2. Value_error
If Value Size exceeds or if data type is mismatched then
this exception occurs
A Number(3):=50000; # Exceeding 4 digit so
Value_Error Occurs
B Number(4):=‘Swapnali’ # DataType Missmatch
Value_Error Occurs
Swapnali Pawar
3. Invalid_Number
This exception occurs if invalid numeric arithmetic
operation is performed
Eg-
‘Swapnali’+10 # Invalid Arithmetic
Operation
Swapnali Pawar
4. No_Data_Found
This Exception occurs if data is not found inTable
N := &roll_no;
Select stud_name # No_Data_Found Exception
From Student
Where roll_no=N;
Swapnali Pawar
5. Too_Many_Rows
This exception occurs if select statement try to fetch
more than 1 record.
Eno := &deptno;
Select ename into EmpName
From employee
Where deptno=Eno;
If 10 records are there for select statement then select statement
fetches all 10 names & are assigned to EmpName but variable
EmpName takes only one at a time so too_many_rows exception
occurs
Swapnali Pawar
6. Dup_Val_On_Index
This exception Occurs if we try to insert duplicate
value in primary key column.
Eg-
Create table MyFriends (id number primary key , name varchar(30));
Insert into MyFriends id values(101);
Insert into MyFriends id values(101);
# 10 already exist in table so Dup_Val_On_Index exception occurs
Swapnali Pawar
7. Cursor_Already_Open
If you try to open cursor which is already open
without closing it then Cursor_Already_Open
exception occurs.
Open c1;
Open c1;
C1 is already open thus Cursor_Already_Open exception occurs.
Swapnali Pawar
8. Invalid_Cursor
If you try to open cursor which is no declared then
Invalid_Cursor exception occurs.
Open C1;
Cursor C1 is not declared & you try to open itThus
Invalid_Cursor exception occurs.
Swapnali Pawar
• User-defined exceptions are declared in a package, subprogram,
or within the declaration section of the PL/SQL block of code
and should be assigned names.
• Once an exception occurs, the natural flow of execution is
halted, and then the execution points to the exception section
of the PL/SQL code.
• User-defined ones have to be thrown explicitly by the RAISE
keyword.
• Thus the exception handling helps to deal with the errors that
are encountered during the run time execution and not while
compiling the program.
User-Defined Exception
Swapnali Pawar
1.Declare a variable of exception data type –
This variable is going to take the entire burden on its shoulders.
2.Raise the Exception –
This is the part where you tell the compiler about the condition
which will trigger the exception.
3.Handle the exception –
This is the last section where you specify what will happen
when the error which you raised will trigger
Steps for Exception Handling
Swapnali Pawar
User-Defined Exception
The developers can build their own exceptions and use them for
handling errors. They can be created in the declaration part of a
subprogram and can be accessed only inside that subprogram.
An exception that is created at the package level can be used
whenever the package is accessed. A user-defined exception can be
raised by using the RAISE keyword.
Syntax -
Declare
Exception_name Exception;
Here, the exception_n is the name of the exception that we are
raising. Thus we can declare an exception by giving a name followed
by the EXCEPTION keyword. An exception can be declared in a
similar manner like variables are declared. However, an exception is
an unexpected condition and not a data item.
Swapnali Pawar
Syntax for Exception Handling
DECLARE
<< declaration section >>
BEGIN
<<Block of executable code>>
IF conditionTHEN
RAISE exception_n;
END IF;
EXCEPTION
WHEN excp1THEN
<< excp1 handling block >>
WHEN excp2 THEN
<< excp2 handling block >>
........
WHEN othersTHEN
<< excp2 handling block>>
END;
The default exception is carried out with WHEN othersTHEN.
Swapnali Pawar
1. Raise Statement
2. Raise_Application_Error Statement
User-Defined Exception
When predefined exceptions don’t satisfy our requirement then
we define our own exception
Swapnali Pawar
1.Using Raise Statement-
Exception is raised using name.This statement is
used if you want to raise exception & also want
to handle it.
2. Using Raise_Application_Error
Statement-
Exception is raised using code.This statement is
used if you want to raise exception but don’t
want to handle it.
Swapnali Pawar
User-Defined Exception
Swapnali Pawar
1.To find sum of two
number:
set serveroutput on;
declare
a int;
b int;
c int;
begin
a:=&a;
b:=&b;
c:=a+b;
dbms_output.put_line('sum
of a and b'||c);
end;
2.To find greatest number among
two:
set serveroutput on
declare
a int;
b int;
c int;
begin
a:=&a;
b:=&b;
if(a>b)
then
dbms_output.put_line('a is greater');
else
dbms_output.put_line('b is greater');
end if;
end;
Declare
a number(2);
b number(2);
c number(4);
one_divide exception;
Begin
a:= &a;
b:= &b;
if b=1 then
raise one_divide;
end if ;
c:= a/b;
dbms_Output.put_line(c);
Exception
when zero_divide then
dbms_output.put_line('zero_Divide');
when one_divide then
dbms_output.put_line('one_divide');
when others then
dbms_output.put_line(sqlerrm);
End; /
Swapnali Pawar
Swapnali Pawar
2. Using Raise_Application_Error Statement-
Declare
vage number(10);
age number:=&vage;
Begin
if age < 18 then
Raise_Application_Error(-20008,'Age must be greater than 18 !');
end if;
dbms_output.put_line(‘Voting Allowed after completing 18 years');
Exception
when others then
dbms_output.put_line(sqlerrm);
End;
/
Swapnali Pawar
Swapnali Pawar
Example.
CREATETABLE CITIZEN (
ID INT NOT NULL,
NAMEVARCHAR (15) NOT NULL,
AGE INT NOT NULL,
PRIMARY KEY (ID)
);
We have created the CITIZEN table with the help of the SQL statement given below.
SELECT * FROM CITIZEN
Swapnali Pawar
Insert values to this table with SQL statements given below:
INSERT INTO CITIZENVALUES (1,‘Swapnali', 26);
INSERT INTO CITIZENVALUES (8,‘Rugveda', 15);
INSERT INTO CITIZENVALUES (5,‘Shree', 37);
Swapnali Pawar
Coding ImplementationWith Exception Handling:
DECLARE
citizen_id citizen.id%type;
citizen_name citizen.name%type;
citizen_age citizen.age%type := 9;
BEGIN
SELECT id, name INTO citizen_id, citizen_name
FROM citizen
WHERE age = citizen_age;
DBMS_OUTPUT.PUT_LINE ('Citizen id is: '|| citizen_id);
DBMS_OUTPUT.PUT_LINE ('Citizen name is: '|| citizen_name);
EXCEPTION
WHEN no_data_foundTHEN
dbms_output.put_line ('No citizen detail found');
WHEN othersTHEN
dbms_output.put_line ('Errors');
END;
/
Swapnali Pawar
Swapnali Pawar
Student Activity
1. Using Raise statement create user defined exception which raise
an error if sum of 2 numbers is greater than 5000.
2. Using Raise statement create user defined exception which raise
an exception if age <18 and displays ‘Your are not Allowed for
Voting This Year ! ’ Message.
3. Using Raise_Application_Error statement create your own
exception code for “Salary must be above 50000 for Loan
Approval !” message.
4. Create table with your name .Add columns Roll_no Name Marks
.Enter some records in table. Execute various queries on that table
& if records are not available raise no_data_found exception.
Swapnali Pawar

Weitere ähnliche Inhalte

Was ist angesagt?

pl/sql Procedure
pl/sql Procedurepl/sql Procedure
pl/sql ProcedurePooja Dixit
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaRaghu nath
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++Jayant Dalvi
 
DBMS lab manual
DBMS lab manualDBMS lab manual
DBMS lab manualmaha tce
 
Conditional statement in c
Conditional statement in cConditional statement in c
Conditional statement in cMuthuganesh S
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling pptJavabynataraJ
 
Data abstraction and object orientation
Data abstraction and object orientationData abstraction and object orientation
Data abstraction and object orientationHoang Nguyen
 
SQL BUILT-IN FUNCTION
SQL BUILT-IN FUNCTIONSQL BUILT-IN FUNCTION
SQL BUILT-IN FUNCTIONArun Sial
 
Different Kinds of Exception in DBMS
Different Kinds of Exception in DBMSDifferent Kinds of Exception in DBMS
Different Kinds of Exception in DBMSSuja Ritha
 
1 - Introduction to PL/SQL
1 - Introduction to PL/SQL1 - Introduction to PL/SQL
1 - Introduction to PL/SQLrehaniltifat
 
04 Handling Exceptions
04 Handling Exceptions04 Handling Exceptions
04 Handling Exceptionsrehaniltifat
 
Stored-Procedures-Presentation
Stored-Procedures-PresentationStored-Procedures-Presentation
Stored-Procedures-PresentationChuck Walker
 
Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming LanguageAhmad Idrees
 
Exception Handling
Exception HandlingException Handling
Exception HandlingReddhi Basu
 

Was ist angesagt? (20)

pl/sql Procedure
pl/sql Procedurepl/sql Procedure
pl/sql Procedure
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++
 
DBMS lab manual
DBMS lab manualDBMS lab manual
DBMS lab manual
 
Conditional statement in c
Conditional statement in cConditional statement in c
Conditional statement in c
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling ppt
 
Data abstraction and object orientation
Data abstraction and object orientationData abstraction and object orientation
Data abstraction and object orientation
 
SQL BUILT-IN FUNCTION
SQL BUILT-IN FUNCTIONSQL BUILT-IN FUNCTION
SQL BUILT-IN FUNCTION
 
Different Kinds of Exception in DBMS
Different Kinds of Exception in DBMSDifferent Kinds of Exception in DBMS
Different Kinds of Exception in DBMS
 
Oracle: Procedures
Oracle: ProceduresOracle: Procedures
Oracle: Procedures
 
1 - Introduction to PL/SQL
1 - Introduction to PL/SQL1 - Introduction to PL/SQL
1 - Introduction to PL/SQL
 
Java program structure
Java program structureJava program structure
Java program structure
 
04 Handling Exceptions
04 Handling Exceptions04 Handling Exceptions
04 Handling Exceptions
 
Stored-Procedures-Presentation
Stored-Procedures-PresentationStored-Procedures-Presentation
Stored-Procedures-Presentation
 
Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming Language
 
Exception handling
Exception handlingException handling
Exception handling
 
4. plsql
4. plsql4. plsql
4. plsql
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 

Ähnlich wie Exception Handling

Oracle - Program with PL/SQL - Lession 08
Oracle - Program with PL/SQL - Lession 08Oracle - Program with PL/SQL - Lession 08
Oracle - Program with PL/SQL - Lession 08Thuan Nguyen
 
7. exceptions handling in pl
7. exceptions handling in pl7. exceptions handling in pl
7. exceptions handling in plAmrit Kaur
 
Exception
ExceptionException
Exceptionwork
 
Exception
ExceptionException
Exceptionwork
 
Oracle PL/SQL exception handling
Oracle PL/SQL exception handlingOracle PL/SQL exception handling
Oracle PL/SQL exception handlingSmitha Padmanabhan
 
Les23[1]Handling Exceptions
Les23[1]Handling ExceptionsLes23[1]Handling Exceptions
Les23[1]Handling Exceptionssiavosh kaviani
 
Introduction to java exceptions
Introduction to java exceptionsIntroduction to java exceptions
Introduction to java exceptionsSujit Kumar
 
Ch-1_5.pdf this is java tutorials for all
Ch-1_5.pdf this is java tutorials for allCh-1_5.pdf this is java tutorials for all
Ch-1_5.pdf this is java tutorials for allHayomeTakele
 
Java SE 11 Exception Handling
Java SE 11 Exception HandlingJava SE 11 Exception Handling
Java SE 11 Exception HandlingAshwin Shiv
 
Exception handler
Exception handler Exception handler
Exception handler dishni
 
Java Exceptions and Exception Handling
 Java  Exceptions and Exception Handling Java  Exceptions and Exception Handling
Java Exceptions and Exception HandlingMaqdamYasir
 
Advanced_SQL_ISASasASasaASnjection (1).ppt
Advanced_SQL_ISASasASasaASnjection (1).pptAdvanced_SQL_ISASasASasaASnjection (1).ppt
Advanced_SQL_ISASasASasaASnjection (1).pptssuserde23af
 
VTU MCA 2022 JAVA Exceeption Handling.docx
VTU MCA  2022 JAVA Exceeption Handling.docxVTU MCA  2022 JAVA Exceeption Handling.docx
VTU MCA 2022 JAVA Exceeption Handling.docxPoornima E.G.
 
Exceptionhandling
ExceptionhandlingExceptionhandling
ExceptionhandlingNuha Noor
 
UNIT III 2021R.pptx
UNIT III 2021R.pptxUNIT III 2021R.pptx
UNIT III 2021R.pptxRDeepa9
 
UNIT III 2021R.pptx
UNIT III 2021R.pptxUNIT III 2021R.pptx
UNIT III 2021R.pptxRDeepa9
 
Itp 120 Chapt 18 2009 Exceptions & Assertions
Itp 120 Chapt 18 2009 Exceptions & AssertionsItp 120 Chapt 18 2009 Exceptions & Assertions
Itp 120 Chapt 18 2009 Exceptions & Assertionsphanleson
 
3963066 pl-sql-notes-only
3963066 pl-sql-notes-only3963066 pl-sql-notes-only
3963066 pl-sql-notes-onlyAshwin Kumar
 

Ähnlich wie Exception Handling (20)

Oracle - Program with PL/SQL - Lession 08
Oracle - Program with PL/SQL - Lession 08Oracle - Program with PL/SQL - Lession 08
Oracle - Program with PL/SQL - Lession 08
 
7. exceptions handling in pl
7. exceptions handling in pl7. exceptions handling in pl
7. exceptions handling in pl
 
Exception
ExceptionException
Exception
 
Exception
ExceptionException
Exception
 
Oracle PL/SQL exception handling
Oracle PL/SQL exception handlingOracle PL/SQL exception handling
Oracle PL/SQL exception handling
 
Les23[1]Handling Exceptions
Les23[1]Handling ExceptionsLes23[1]Handling Exceptions
Les23[1]Handling Exceptions
 
Introduction to java exceptions
Introduction to java exceptionsIntroduction to java exceptions
Introduction to java exceptions
 
Ch-1_5.pdf this is java tutorials for all
Ch-1_5.pdf this is java tutorials for allCh-1_5.pdf this is java tutorials for all
Ch-1_5.pdf this is java tutorials for all
 
Plsql guide 2
Plsql guide 2Plsql guide 2
Plsql guide 2
 
Java SE 11 Exception Handling
Java SE 11 Exception HandlingJava SE 11 Exception Handling
Java SE 11 Exception Handling
 
Exception handler
Exception handler Exception handler
Exception handler
 
Java Exceptions and Exception Handling
 Java  Exceptions and Exception Handling Java  Exceptions and Exception Handling
Java Exceptions and Exception Handling
 
Alerts in r12
Alerts in r12Alerts in r12
Alerts in r12
 
Advanced_SQL_ISASasASasaASnjection (1).ppt
Advanced_SQL_ISASasASasaASnjection (1).pptAdvanced_SQL_ISASasASasaASnjection (1).ppt
Advanced_SQL_ISASasASasaASnjection (1).ppt
 
VTU MCA 2022 JAVA Exceeption Handling.docx
VTU MCA  2022 JAVA Exceeption Handling.docxVTU MCA  2022 JAVA Exceeption Handling.docx
VTU MCA 2022 JAVA Exceeption Handling.docx
 
Exceptionhandling
ExceptionhandlingExceptionhandling
Exceptionhandling
 
UNIT III 2021R.pptx
UNIT III 2021R.pptxUNIT III 2021R.pptx
UNIT III 2021R.pptx
 
UNIT III 2021R.pptx
UNIT III 2021R.pptxUNIT III 2021R.pptx
UNIT III 2021R.pptx
 
Itp 120 Chapt 18 2009 Exceptions & Assertions
Itp 120 Chapt 18 2009 Exceptions & AssertionsItp 120 Chapt 18 2009 Exceptions & Assertions
Itp 120 Chapt 18 2009 Exceptions & Assertions
 
3963066 pl-sql-notes-only
3963066 pl-sql-notes-only3963066 pl-sql-notes-only
3963066 pl-sql-notes-only
 

Mehr von Swapnali Pawar

Unit 3 introduction to android
Unit 3 introduction to android Unit 3 introduction to android
Unit 3 introduction to android Swapnali Pawar
 
Unit 1-Introduction to Mobile Computing
Unit 1-Introduction to Mobile ComputingUnit 1-Introduction to Mobile Computing
Unit 1-Introduction to Mobile ComputingSwapnali Pawar
 
Unit 2.design mobile computing architecture
Unit 2.design mobile computing architectureUnit 2.design mobile computing architecture
Unit 2.design mobile computing architectureSwapnali Pawar
 
Fresher interview tips demo
Fresher interview tips demoFresher interview tips demo
Fresher interview tips demoSwapnali Pawar
 
Introduction to android
Introduction to androidIntroduction to android
Introduction to androidSwapnali Pawar
 
Unit 2.design computing architecture 2.1
Unit 2.design computing architecture 2.1Unit 2.design computing architecture 2.1
Unit 2.design computing architecture 2.1Swapnali Pawar
 
Unit 2 Design mobile computing architecture MC1514
Unit 2 Design mobile computing architecture MC1514Unit 2 Design mobile computing architecture MC1514
Unit 2 Design mobile computing architecture MC1514Swapnali Pawar
 
Design computing architecture ~ Mobile Technologies
Design computing architecture ~ Mobile TechnologiesDesign computing architecture ~ Mobile Technologies
Design computing architecture ~ Mobile TechnologiesSwapnali Pawar
 
Mobile technology-Unit 1
Mobile technology-Unit 1Mobile technology-Unit 1
Mobile technology-Unit 1Swapnali Pawar
 
Web Programming& Scripting Lab
Web Programming& Scripting LabWeb Programming& Scripting Lab
Web Programming& Scripting LabSwapnali Pawar
 
Database Management System 1
Database Management System 1Database Management System 1
Database Management System 1Swapnali Pawar
 
web programming & scripting 2
web programming & scripting 2web programming & scripting 2
web programming & scripting 2Swapnali Pawar
 
web programming & scripting
web programming & scriptingweb programming & scripting
web programming & scriptingSwapnali Pawar
 

Mehr von Swapnali Pawar (20)

Unit 3 introduction to android
Unit 3 introduction to android Unit 3 introduction to android
Unit 3 introduction to android
 
Unit 1-Introduction to Mobile Computing
Unit 1-Introduction to Mobile ComputingUnit 1-Introduction to Mobile Computing
Unit 1-Introduction to Mobile Computing
 
Unit 2.design mobile computing architecture
Unit 2.design mobile computing architectureUnit 2.design mobile computing architecture
Unit 2.design mobile computing architecture
 
Introduction to ios
Introduction to iosIntroduction to ios
Introduction to ios
 
Fresher interview tips demo
Fresher interview tips demoFresher interview tips demo
Fresher interview tips demo
 
View & index in SQL
View & index in SQLView & index in SQL
View & index in SQL
 
Introduction to android
Introduction to androidIntroduction to android
Introduction to android
 
Android Introduction
Android IntroductionAndroid Introduction
Android Introduction
 
SQL JOINS
SQL JOINSSQL JOINS
SQL JOINS
 
Unit 2.design computing architecture 2.1
Unit 2.design computing architecture 2.1Unit 2.design computing architecture 2.1
Unit 2.design computing architecture 2.1
 
Unit 2 Design mobile computing architecture MC1514
Unit 2 Design mobile computing architecture MC1514Unit 2 Design mobile computing architecture MC1514
Unit 2 Design mobile computing architecture MC1514
 
Design computing architecture ~ Mobile Technologies
Design computing architecture ~ Mobile TechnologiesDesign computing architecture ~ Mobile Technologies
Design computing architecture ~ Mobile Technologies
 
Mobile technology-Unit 1
Mobile technology-Unit 1Mobile technology-Unit 1
Mobile technology-Unit 1
 
Mobile Technology 3
Mobile Technology 3Mobile Technology 3
Mobile Technology 3
 
Web Programming& Scripting Lab
Web Programming& Scripting LabWeb Programming& Scripting Lab
Web Programming& Scripting Lab
 
Mobile Technology
Mobile TechnologyMobile Technology
Mobile Technology
 
Mobile Technology
Mobile TechnologyMobile Technology
Mobile Technology
 
Database Management System 1
Database Management System 1Database Management System 1
Database Management System 1
 
web programming & scripting 2
web programming & scripting 2web programming & scripting 2
web programming & scripting 2
 
web programming & scripting
web programming & scriptingweb programming & scripting
web programming & scripting
 

Kürzlich hochgeladen

Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 

Kürzlich hochgeladen (20)

Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 

Exception Handling

  • 1. Database Lab Swapnali Pawar Predefined & User Defined Exceptions Swapnali Pawar
  • 2. • An exception is an error condition during a program execution.An error or a warning event is called an exception. • Any abnormal condition or say event that interrupts the normal flow of your program’s instructions at run time is an exception. Or in simple words you can say an exception is a run time error. • Exceptions are designed for run time error handling rather than compile time error handling. Errors that occur during compilation phase are detected by the PL/SQL compiler and reported back to the user. What is an Exception? Swapnali Pawar
  • 5. There are two types of PL/SQL exceptions in Oracle database. 1. PreDefined / System-defined Exceptions 2. User-Defined Exceptions Types of Exceptions Swapnali Pawar
  • 6. • System-defined exceptions are defined and maintained implicitly by the Oracle server.These exceptions are mainly defined in the Oracle STANDARD package.Whenever an exception occurs inside the program.The Oracle server matches and identifies the appropriate exception from the available set of exceptions. • System defined exceptions majorly have a negative error code and error message.These errors have a short name which is used with the exception handler. 1. System-Defined Exceptions Swapnali Pawar
  • 7. Unlike System-Define Exception, User-Define Exceptions are raised explicitly in the body of the PL/SQL block i.e more specifically inside the BEGIN-END section)using the RAISE Statement. 2. User-Define Exceptions Swapnali Pawar
  • 8. • If our code does not have an exception handling, then each time we execute a statement, we must verify errors in execution . • If we avoid exception handling in our code, the actual errors get missed which gives rise to some other errors. • The exception handling allows skipping multiple verifications in the code. • It provides better readability of the code by isolating the error handlers in the code Advantages Of Exception Handling Swapnali Pawar
  • 9. 1. Pre-Defined Exception • system defined exceptions are thrown by default. • Some of the popular System defined exceptions are out of memory and division by zero, having names like STORAGE_ERROR and ZERO_DIVIDE respectively. Swapnali Pawar
  • 10. Exception Oracle Error SQL CODE Description ACCESS_INTO_NULL ORA - 06530 -6530 This exception is raised if a null object is naturally assigned a value. CASE_NOT_FOUND ORA - 06592 -6592 This exception is raised if none of the options in theWHEN clause is chosen and there is no existence of an ELSE clause. COLLECTION_IS_NULL ORA - 06531 -6531 This exception is raised when the code tries to apply collection methods except EXISTS to a nested table or varray which is not initialized. It can also be raised if our code tries to assign values to a nested table or varray which is not initialized. DUP_VAL_ON_INDEX ORA - 00001 -1 This exception is raised if duplicate values are tried to be stored in a column that is constrained by a unique index. CURSOR_ALREADY_OP EN ORA - 06511 -6511 This exception is raised if our code tries to open an already open cursor. INVALID_CURSOR ORA - 01001 -1001 This exception is raised if we try to do some operations on cursors which are not permitted.For example, attempting to close an already closed cursor. INVALID_NUMBER ORA - 01722 -1722 This exception is raised if the conversion to a character string to a number does not pass as the string is representing an invalid number. Pre-Defined Exception Swapnali Pawar
  • 11. Exception Oracle Error SQL CODE Description SYS_INVALID_ROWID ORA-01410 -1410 This exception is raised if the conversion to a character string to a universal row id does not pass as the character string is representing an invalid row id. TIMEOUT_ON_RESOUR CE ORA-00051 -51 This exception is raised if Oracle is waiting for a resource. VALUE_ERROR ORA-06502 -6502 This exception is raised if a mathematical, conversion, truncation error happens in our program. ZERO_DIVIDE ORA-01476 -1476 This exception is raised if we try to divide a number by 0. Swapnali Pawar
  • 12. 1. Pre-Defined Exception Exceptions Defined by Oracle i.e System Defined Exceptions- 1 . Zero_divide 2 . value_error 3 . Invalid_Number 4 . N0_Data_Found 5 . Too_Many_Rows 6 . Dup_Val_On_Index 7 . Cursor_Already_Open 8 . Invalid_Cursor Swapnali Pawar
  • 13. This Exception occurs if any number is divided by Zero . Eg- a := &a; # 16 b := &b; # 0 c := a/b; # Occurs Divide_Zero Exception 1. Zero_divide Swapnali Pawar
  • 14. 2. Value_error If Value Size exceeds or if data type is mismatched then this exception occurs A Number(3):=50000; # Exceeding 4 digit so Value_Error Occurs B Number(4):=‘Swapnali’ # DataType Missmatch Value_Error Occurs Swapnali Pawar
  • 15. 3. Invalid_Number This exception occurs if invalid numeric arithmetic operation is performed Eg- ‘Swapnali’+10 # Invalid Arithmetic Operation Swapnali Pawar
  • 16. 4. No_Data_Found This Exception occurs if data is not found inTable N := &roll_no; Select stud_name # No_Data_Found Exception From Student Where roll_no=N; Swapnali Pawar
  • 17. 5. Too_Many_Rows This exception occurs if select statement try to fetch more than 1 record. Eno := &deptno; Select ename into EmpName From employee Where deptno=Eno; If 10 records are there for select statement then select statement fetches all 10 names & are assigned to EmpName but variable EmpName takes only one at a time so too_many_rows exception occurs Swapnali Pawar
  • 18. 6. Dup_Val_On_Index This exception Occurs if we try to insert duplicate value in primary key column. Eg- Create table MyFriends (id number primary key , name varchar(30)); Insert into MyFriends id values(101); Insert into MyFriends id values(101); # 10 already exist in table so Dup_Val_On_Index exception occurs Swapnali Pawar
  • 19. 7. Cursor_Already_Open If you try to open cursor which is already open without closing it then Cursor_Already_Open exception occurs. Open c1; Open c1; C1 is already open thus Cursor_Already_Open exception occurs. Swapnali Pawar
  • 20. 8. Invalid_Cursor If you try to open cursor which is no declared then Invalid_Cursor exception occurs. Open C1; Cursor C1 is not declared & you try to open itThus Invalid_Cursor exception occurs. Swapnali Pawar
  • 21. • User-defined exceptions are declared in a package, subprogram, or within the declaration section of the PL/SQL block of code and should be assigned names. • Once an exception occurs, the natural flow of execution is halted, and then the execution points to the exception section of the PL/SQL code. • User-defined ones have to be thrown explicitly by the RAISE keyword. • Thus the exception handling helps to deal with the errors that are encountered during the run time execution and not while compiling the program. User-Defined Exception Swapnali Pawar
  • 22. 1.Declare a variable of exception data type – This variable is going to take the entire burden on its shoulders. 2.Raise the Exception – This is the part where you tell the compiler about the condition which will trigger the exception. 3.Handle the exception – This is the last section where you specify what will happen when the error which you raised will trigger Steps for Exception Handling Swapnali Pawar
  • 23. User-Defined Exception The developers can build their own exceptions and use them for handling errors. They can be created in the declaration part of a subprogram and can be accessed only inside that subprogram. An exception that is created at the package level can be used whenever the package is accessed. A user-defined exception can be raised by using the RAISE keyword. Syntax - Declare Exception_name Exception; Here, the exception_n is the name of the exception that we are raising. Thus we can declare an exception by giving a name followed by the EXCEPTION keyword. An exception can be declared in a similar manner like variables are declared. However, an exception is an unexpected condition and not a data item. Swapnali Pawar
  • 24. Syntax for Exception Handling DECLARE << declaration section >> BEGIN <<Block of executable code>> IF conditionTHEN RAISE exception_n; END IF; EXCEPTION WHEN excp1THEN << excp1 handling block >> WHEN excp2 THEN << excp2 handling block >> ........ WHEN othersTHEN << excp2 handling block>> END; The default exception is carried out with WHEN othersTHEN. Swapnali Pawar
  • 25. 1. Raise Statement 2. Raise_Application_Error Statement User-Defined Exception When predefined exceptions don’t satisfy our requirement then we define our own exception Swapnali Pawar
  • 26. 1.Using Raise Statement- Exception is raised using name.This statement is used if you want to raise exception & also want to handle it. 2. Using Raise_Application_Error Statement- Exception is raised using code.This statement is used if you want to raise exception but don’t want to handle it. Swapnali Pawar User-Defined Exception
  • 27. Swapnali Pawar 1.To find sum of two number: set serveroutput on; declare a int; b int; c int; begin a:=&a; b:=&b; c:=a+b; dbms_output.put_line('sum of a and b'||c); end; 2.To find greatest number among two: set serveroutput on declare a int; b int; c int; begin a:=&a; b:=&b; if(a>b) then dbms_output.put_line('a is greater'); else dbms_output.put_line('b is greater'); end if; end;
  • 28. Declare a number(2); b number(2); c number(4); one_divide exception; Begin a:= &a; b:= &b; if b=1 then raise one_divide; end if ; c:= a/b; dbms_Output.put_line(c); Exception when zero_divide then dbms_output.put_line('zero_Divide'); when one_divide then dbms_output.put_line('one_divide'); when others then dbms_output.put_line(sqlerrm); End; / Swapnali Pawar
  • 30. 2. Using Raise_Application_Error Statement- Declare vage number(10); age number:=&vage; Begin if age < 18 then Raise_Application_Error(-20008,'Age must be greater than 18 !'); end if; dbms_output.put_line(‘Voting Allowed after completing 18 years'); Exception when others then dbms_output.put_line(sqlerrm); End; / Swapnali Pawar
  • 32. Example. CREATETABLE CITIZEN ( ID INT NOT NULL, NAMEVARCHAR (15) NOT NULL, AGE INT NOT NULL, PRIMARY KEY (ID) ); We have created the CITIZEN table with the help of the SQL statement given below. SELECT * FROM CITIZEN Swapnali Pawar Insert values to this table with SQL statements given below: INSERT INTO CITIZENVALUES (1,‘Swapnali', 26); INSERT INTO CITIZENVALUES (8,‘Rugveda', 15); INSERT INTO CITIZENVALUES (5,‘Shree', 37);
  • 33. Swapnali Pawar Coding ImplementationWith Exception Handling: DECLARE citizen_id citizen.id%type; citizen_name citizen.name%type; citizen_age citizen.age%type := 9; BEGIN SELECT id, name INTO citizen_id, citizen_name FROM citizen WHERE age = citizen_age; DBMS_OUTPUT.PUT_LINE ('Citizen id is: '|| citizen_id); DBMS_OUTPUT.PUT_LINE ('Citizen name is: '|| citizen_name); EXCEPTION WHEN no_data_foundTHEN dbms_output.put_line ('No citizen detail found'); WHEN othersTHEN dbms_output.put_line ('Errors'); END; /
  • 35. Swapnali Pawar Student Activity 1. Using Raise statement create user defined exception which raise an error if sum of 2 numbers is greater than 5000. 2. Using Raise statement create user defined exception which raise an exception if age <18 and displays ‘Your are not Allowed for Voting This Year ! ’ Message. 3. Using Raise_Application_Error statement create your own exception code for “Salary must be above 50000 for Loan Approval !” message. 4. Create table with your name .Add columns Roll_no Name Marks .Enter some records in table. Execute various queries on that table & if records are not available raise no_data_found exception.