[go: up one dir, main page]

0% found this document useful (0 votes)
28 views34 pages

Exception Handling

Exception

Uploaded by

Sailesh Sailesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views34 pages

Exception Handling

Exception

Uploaded by

Sailesh Sailesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

10.

Exception Handling

Sushant Bhattarai
Types of Error

1. Syntax Error:
It occurs due to typing mistake or by writing
wrong syntax.
These can be corrected as they are
detected by compiler.
Types of Error

2. Logical Error
 These kind of errors are hard to debug
because the program compiles and
executes successfully but produces
erroneous result.
Types of Error

3. Linker Error
 Sometimes syntax and logic are both
correct but during linking error occurs due
to wrong function prototyping and
incorrect header files.
Types of Error

4. Run Time Error


 It occurs during execution of the program.
 It compiles and executes successfully but
error is generated when the program is
running.
10.1 Introduction to
Exceptions.

Er.Sushant Bhatta
Exceptions

 An exception is an abnormal condition that arises in a program at


runtime.
 E.g.
#include <iostream>
using namespace std;
int main()
{
float a,b;
cout<<“Enter a num :”;
cin>>a;
b=100/a;
cout<<“The result is ”<<b<<endl;
return 0;
}
Exceptions

 Abnormal behavior should be handled by the


programmer.
 The ability to deal with a program’s eventual
abnormal behavior is called exception handling.
Exceptions

 Some abnormal conditions are:


1. Divide by 0.
2. Short of memory.
3. Trying to open non-existing file in input mode.
Exception Handling
Model
 The error handling mechanism performs
the following tasks.
1. Find the problem(Hit the exception).
2. Inform that the error has occurred(Throw
the exception).
3. Receive the error information(Catch the
exception).
10.2 Benefits of Exception
handling.

Er.Sushant Bhatta
10.3 Try and catch block.

Er.Sushant Bhatta
try Block

 All the statements in main() that might cause the


exceptions are enclosed in braces and preceded by the
try keyword:
try
{
//code that might cause an exception
}
 Not all the code in the program needs to be in a try block;
just the code that might cause an exception.
 Also, there can be many try blocks in your program.

catch Block
 The code that handles the exception is enclosed in braces, preceded
by the catch keyword, with the exception class name in parentheses.
 The exception class name must include the class in which it is
located.
catch(class::exception_class)
{
//code that handles the exception
}
 This construction is called the exception handler.
 It must immediately follow the try block.
 Control “falls through” the bottom of the exception handler, so you
can continue processing at that point.
 Or the exception handler may transfer control elsewhere, or (often)
terminate the program.
10.4 Throw statement.

Er.Sushant Bhatta
Throw Block

 We throw exception from try block to catch


block using throw keyword.
 It can throw objects of different types.
 If the type of object thrown matches the
argument type in catch statement, the
catch block will run.
 If they do not then program will be aborted.

Example

 Write a program to use the exception


handling for divide by zero error.
Example
Example
Example
Example

 Write a program to read content of a file


and use exception handling if the file
doesn’t exist.
Example
Multiple Catch Blocks

 There can be multiple catch blocks to


receive the multiple exceptions that are
thrown.

Multiple Catch Blocks

 WAP to illustrate the use of multiple catch


blocks.
Multiple Catch Blocks
Multiple Catch Blocks
Multiple Catch Blocks
Multiple Catch Blocks
Catching all types of exceptions by
single catch block

 We can catch all types of exceptions using


a single catch block.
 This is done as follows:
catch(…)
{

}
Catching all types of exceptions by
single catch block
Catching all types of exceptions by
single catch block
Catching all types of exceptions by
single catch block
Stack Unwinding.
 The process of removing function entries from function
call stack at run time is called Stack Unwinding.
 Stack Unwinding is generally related to Exception
Handling.
 In C++, when an exception occurs, the function call stack
is linearly searched for the exception handler, and all the
entries before the function with exception handler are
removed from the function call stack.
 So exception handling involves Stack Unwinding if
exception is not handled in same function (where it is
thrown).

You might also like