Exception Handling: B. L. Patil Polytechnic, Khopoli

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

K.T.S.P.

MANDAL’S

B. L. PATIL POLYTECHNIC, KHOPOLI.

A
PROJECT
REPORT ON

Exception Handling

A project work submitted to the MSBTE, Mumbai region

In partial fulfillment of the requirements for Diploma in Computer Engineering.

Submitted by

2200360055 Mr.Shubham.c.Kathavale CO3I

1
Under the Guidance
of Mrs.shitel
mohite
DEPARTMENT OF COMPUTER ENGINEERING

2
. B. L. PATIL POLYTECHNIC, KHOPOLI.

Khopoli, Raigad – 410203

DEPARTMENT OF COMPUTER ENGINEERING

CERTIFICATE

This is to certify that the seminar report entitled

By

2200360055 Mr. Shubham.c.Kathavale CO3I

This is a record of bonafide work carried out by him, for project report of
semester in academic year 2022-2023 in the Department of Computer
Engineering of B. L. Patil Polytechnic, Khopoli of Maharashtra State Board of
Technical Education, Mumbai.
Date:
Place:

Mrs.shitel mohite Mr.S.L.Murade

(Project Guide) (H.O.D.)

External Examiner Seal Prof. P. S.


3
Mane. (principal).

ABSTRACT
removal of entities from the other end of the sequence. By convention, the end of the
sequence at which elements are added is called the back, tail, or rear of the queue, and
the end at which elements are removed is called the head or front of the queue,
analogously to the words used when people line up to wait for goods or service.

4
INDEX

Sr No. Topic Page No.

1 What Is 5
Exception
2 History 6

3 Advantages Of 6
Using Exception
4 Dis-Advantages Of 7
Using Exception
5 Example 9

6 Pl/Sql User Defined 13


Exception

5
What is Exception
In computing and computer programming, exception handling is the process
of responding to the occurrence of exceptions – anomalous or exceptional
conditions requiring special processing – during the execution of a program.
In general, an exception breaks the normal flow of execution and executes a
pre-registered exception handler; the details of how this is done depend on
whether it is a hardware or software exception and how the software
exception is implemented. Exception handling, if provided, is facilitated by
specialized programming language constructs, hardware mechanisms
like interrupts, or operating system (OS) inter-process communication (IPC)
facilities like signals. Some exceptions, especially hardware ones, may be
handled so gracefully that execution can resume where it was interrupted.
PL/SQL facilitates programmers to catch such conditions using
exception block in the program and an appropriate action is taken
against the error condition.

There are two type of exceptions:

o System-defined Exceptions
o User-defined Exceptions

History
Main articles: Interrupt § History, IEEE 754 § History,

and Exception handling (programming) § History

The first hardware exception handling was found in


the UNIVAC I from 1951. Arithmetic overflow executed
two instructions at address 0 which could transfer control
or fix up the result.[10] Software exception handling
developed in the 1960s and 1970s. Exception handling was
6
subsequently widely adopted by many programming
languages from the 1980s onward.’

Advantages of Using Exceptions:


1. **Clarity and Readability**: Exceptions provide a
more intuitive way to handle errors. Exception-
handling code is typically separated from the main
flow of the program, making the main code easier
to read and understand.
2. **Separation of Concerns**: Exceptions allow you
to separate error-handling logic from the normal
flow of the program. This makes the codebase
cleaner and more organized.
3. **Stack Unwinding**: When an exception is
thrown, the call stack is unwound, allowing the
program to jump directly to the appropriate
exception handler. This can lead to faster error
recovery and better debugging.
4. **No Need for Global Error Checks**: You don't
need to explicitly check for errors after every
function call or operation, reducing clutter in the
code and avoiding the need for cascading error
codes.
5. **Consistent Handling**: Using exceptions
ensures that errors are consistently handled across
7
the application, promoting a more uniform
approach to error management.

Disadvantages of Using Exceptions:


1. **Performance Overhead**: Throwing and catching exceptions
can introduce performance overhead, especially in situations where
exceptions are thrown frequently.

2. **Resource Management**: Exceptions can complicate resource


management (e.g., memory, file handles) since resources might not
be released properly if an exception is thrown within a try block.

3. **Complex Control Flow**: Exception handling can introduce


complex control flow, especially if exceptions are nested or caught
at different levels of the call stack.

4. **Misuse of Exceptions**: Overusing exceptions for flow control


or non-exceptional situations can lead to confusing and error-
prone code.

**Advantages of Using Error Codes:**

1. **Performance**: Using error codes can be more efficient than


exceptions, as there is no overhead associated with stack
unwinding or object creation. 8
2. **Simplicity**: Error codes provide a straightforward way to
indicate errors and handle them using standard control flow
constructs like if statements.

3. **Resource Management**: With error codes, you have more


control over resource management, as you can explicitly release
resources in case of errors.

In programming language
This section is an excerpt from Exception handling

(programming).

In computer programming, several language mechanisms


exist for exception handling. The term exception is typically
used to denote a data structure storing information about
an exceptional condition. One mechanism to transfer 9
control, or raise an exception, is known as a throw; the
exception is said to be thrown. Execution is transferred to
a catch.
Programming languages differ substantially in their notion
of what an exception is. Contemporary languages can
roughly be divided into two groups: [15][note 1]

 Languages where exceptions are designed to be used as

flow control structures: Ada, Modula-3, ML, OCaml, PL/I,

Python, and Ruby fall in this category. For

example, Python's iterators throw StopIteration

exceptions to signal that there are no further items

produced by the iterator.[16]

 Languages where exceptions are only used to handle

abnormal, unpredictable, erroneous situations: C++,

[17] Java,[18] C#, Common Lisp, Eiffel, and Modula-2.

10
PL/SQL Exception Handling
Syntax for exception handling:

Following is a general syntax for exception handling:

1. DECLARE
2. <declarations section>
3. BEGIN
4. <executable command(s)>
5. EXCEPTION
6. <exception handling goes here >
7. WHEN exception1 THEN
8. exception1-handling-statements
9. WHEN exception2 THEN
10. exception2-handling-statements
11. WHEN exception3 THEN
12. exception3-handling-statements 11
13. ........
14. WHEN others THEN
15. exception3-handling-statements
16. END;

EXAMPLE OF EXPECTION HANDLING


Let's take a simple example to demonstrate the concept of exception handling. Here we
are using the already created CUSTOMERS table.

SELECT* FROM COUSTOMERS;

ID NAME AGE ADDRESS SALARY

1 Ramesh 23 Allahabad 20000

2 Suresh 22 Kanpur 22000

3 Mahesh 24 Ghaziabad 24000

4 Chandan 25 Noida 26000

5 Alex 21 Paris 28000

6 Sunita 20 Delhi 30000

1. DECLARE 12
2. c_id customers.id%type := 8;
3. c_name customers.name%type;
4. c_addr customers.address%type;
5. BEGIN
6. SELECT name, address INTO c_name, c_addr
7. FROM customers
8. WHERE id = c_id;
9. DBMS_OUTPUT.PUT_LINE ('Name: '|| c_name);
10. DBMS_OUTPUT.PUT_LINE ('Address: ' || c_addr);
11. EXCEPTION
12. WHEN no_data_found THEN
13. dbms_output.put_line('No such customer!');
14. WHEN others THEN
15. dbms_output.put_line('Error!');
16. END;
17. /

After the execution of above code at SQL Prompt, it produces the following result:

No such customer!
PL/SQL procedure successfully completed.
The above program should show the name and address of a customer as result whose ID
is given. But there is no customer with ID value 8 in our database, so the program raises
the run-time exception NO_DATA_FOUND, which is captured in EXCEPTION block.

If you use the id defined in the above table (i.e. 1 to 6), you will get a certain result. For a
demo example: here, we are using the id 5.

1. DECLARE
2. c_id customers.id%type := 5;
3. c_name customers.name%type;
4. c_addr customers.address%type;
5. BEGIN
6. SELECT name, address INTO c_name, c_addr
7. FROM customers
8. WHERE id = c_id;
9. DBMS_OUTPUT.PUT_LINE ('Name: '|| c_name);
10. DBMS_OUTPUT.PUT_LINE ('Address: ' || c_addr);
11. EXCEPTION
12. WHEN no_data_found THEN
13. dbms_output.put_line('No such customer!');
14. WHEN others THEN 13
15. dbms_output.put_line('Error!');
16. END;
17. /

After the execution of above code at SQL prompt, you will get the following result:

Name: alex
Address: paris
PL/SQL procedure successfully completed.
AD

Raising Exceptions
In the case of any internal database error, exceptions are raised by the database server
automatically. But it can also be raised explicitly by programmer by using command
RAISE.

Syntax for raising an exception:

1. DECLARE
2. exception_name EXCEPTION;
3. BEGIN
4. IF condition THEN
5. RAISE exception_name;
6. END IF;
7. EXCEPTION
8. WHEN exception_name THEN
9. statement;
10. END;

14
PL/SQL User-defined Exceptions
PL/SQL facilitates their users to define their own exceptions according to the need of the
program. A user-defined exception can be raised explicitly, using either a RAISE
statement or the procedure DBMS_STANDARD.RAISE_APPLICATION_ERROR.

Syntax for user define exceptions

1. DECLARE
2. my-exception EXCEPTION;

15
PL/SQL Pre-defined Exceptions
There are many pre-defined exception in PL/SQL which are executed when any database
rule is violated by the programs.

AD

For example: NO_DATA_FOUND is a pre-defined exception which is raised when a


SELECT INTO statement returns no rows.

Following is a list of some important pre-defined exceptions:

Exception Oracle SQL Description

Error Code

ACCESS_INTO_NULL 06530 -6530 It is raised when a NULL object is automatically assigned a


value.

CASE_NOT_FOUND 06592 -6592 It is raised when none of the choices in the "WHEN" clauses of
a CASE statement is selected, and there is no else clause.

COLLECTION_IS_NULL 06531 -6531 It is raised when a program attempts to apply collection


methods other than exists to an uninitialized nested table or
varray, or the program attempts to assign values to the
elements of an uninitialized nested table or varray.

DUP_VAL_ON_INDEX 00001 -1 It is raised when duplicate values are attempted to be stored


in a column with unique index.

INVALID_CURSOR 01001 -1001 It is raised when attempts are made to make a cursor
operation that is not allowed, such as closing an unopened
cursor.

INVALID_NUMBER 01722 -1722 It is raised when the conversion of a character string into a
number fails because the string does not represent a valid
16
number.
LOGIN_DENIED 01017 -1017 It is raised when s program attempts to log on to the
database with an invalid username or password.

NO_DATA_FOUND 01403 +100 It is raised when a select into statement returns no rows.

NOT_LOGGED_ON 01012 -1012 It is raised when a database call is issued without being
connected to the database.

PROGRAM_ERROR 06501 -6501 It is raised when PL/SQL has an internal problem.

ROWTYPE_MISMATCH 06504 -6504 It is raised when a cursor fetches value in a variable having
incompatible data type.

SELF_IS_NULL 30625 -30625 It is raised when a member method is invoked, but the
instance of the object type was not initialized.

STORAGE_ERROR 06500 -6500 It is raised when PL/SQL ran out of memory or memory was
corrupted.

TOO_MANY_ROWS 01422 -1422 It is raised when a SELECT INTO statement returns more than
one row.

VALUE_ERROR 06502 -6502 It is raised when an arithmetic, conversion, truncation, or size-


constraint error occurs.

ZERO_DIVIDE 01476 1476 It is raised when an attempt is made to divide a number by


zero.

17

You might also like