The document discusses exception handling in C++. It defines an exception as an error that occurs during runtime. C++ includes a built-in exception handling mechanism using try, catch, and throw keywords. Code that can generate exceptions is placed in a try block. If an exception occurs, it is thrown using throw and caught using catch, where it can be processed. Exceptions are objects belonging to a specific class. Multiple catch blocks can handle different exception types in order. A catch-all block using ellipses (...) can handle any exception.
Introduction to G. H. Raisoni College's IT Department, focusing on Object Oriented Programming, particularly Exception Handling with C++.
An exception is an unexpected event during program execution. It occurs due to invalid user input, like negative numbers in a factorial program, requiring exception handling.
Highlights common failures: memory allocation issues, division by zero, invalid parameters. C++ has a built-in exception handling mechanism unlike C.
Overview of key C++ keywords in exception handling: try, catch, throw. Clean error management through structured programming is emphasized.
A real-world analogy comparing Smartphone testing procedures to exception handling mechanisms: try, throw, catch.
Key points for using try-catch blocks effectively in C++. Necessity of structure: try should precede catch; improper usage leads to errors.
Definition and usage of try block in managing code errors in C++. Syntax for throwing exceptions is introduced.
Example program demonstrating a try-catch block handling division and exception throwing based on conditions.
Different methods of throwing exceptions using throw statements. Exploration of condition-based multiple throw statements.
Implementing exception throwing within functions and managing it via try-catch in the main program to simplify control flow.
Description of a catch block's function in exception handling, specifying type matching and potential termination without a match.
Syntax for implementing multiple catch statements for better exception handling and error management in C++.
Process of searching for matching exception handlers in multiple catch statements, and behavior if no match is found.
A method of utilizing a catch-all statement to handle all types of exceptions, increasing flexibility in error management.
G. H. RAISONICOLLEGE OF ENGINEERING
(AN AUTONOMOUS INSTITUTE AFFILIATED BY R. T. M. NAGPUR UNIVERSITY UNDER UGC
ACT 1956)
DEPARTMENT OF INFORMATION TECHNOLOGY
SESSION 2016-2017
Object Oriented Programming
Through C++
TAE-2
Topic: exception handling in c++
Deepak tathe (37)
2.
WHAT IS ANEXCEPTION ?
It is often easier to write a program by first assuming that
nothing incorrect will happen
But sometimes user enters an Invalid value e.g. In a
factorial program if a user enters any negative value.
For such cases Exception Handling is used.
Using Exception Handling you can easily manage and
respond to run-time error.
3.
COMMON FAILURES
newnot allocating memory
Division by zero
Invalid function parameters
C does not have Exception Handling Mechanism
But, C++ have build in Exception Handling
Mechanism
We can handle exception without exception handling
but that will not be a professional and systematic
approach and it will not clear the program logic.
4.
In Exceptionhandling we use three keywords try , catch,
throw.
Program statements you want to monitor for exceptions are
contained in a try box
If an exception occurs within the try block it is thrown
The exception is caught using Catch and processed.
Try , Catch , Throw
IMPORTANT POINTS
Whena try block end it must be followed by a catch block.
If we write catch block but don’t write try block then the program
will show
If we don’t write try and catch but write throw then the program
will build and run but it will show run time error.
#include<iostream>
main()
{
std::cout<<"welcome";
throw 10;
return 0;
}
7.
TRY BLOCK
Inan program a block of code which can produce error
is placed in try block
When an exception is detected in try it is thrown using
throw statement
Syntax
try
{
throw exception;
}
catch(type argument)
{
}
8.
#include<iostream>
using namespace std;
main()
{
inta,b;
cout<<"enter values of a and bn";
cin>>a;
cin>>b;
int x=a-b;
try{
if (x!=0)
{
cout<<"result(a/x)="<<a/x<<"n";
}
else{
throw(x);
}
}
catch(int i)
{
cout<<"exception
caught:x="<<x<<"n";
}
cout<<"end";
return 0;
}
9.
THROWING MECHANISM
Anexception is thrown using the throw statement in one of the following
ways
throw(exception);
throw exception;
throw;
o We can add use multiple throw statements by using if – else
statement
e.g.
int i=3;
Try {
if(i==1)
throw 1;
if(i==2)
throw 2;
}
10.
CALL THROW USINGFUNCTION
#include<iostream>
using namespace std;
void fun()
{
throw 3;
}
int main()
{
int i=3;
try{
if (i=3)
fun();
}
catch(int e){
cout<<"n exception no:"<<e;
}
cout<<"n throw successfully called by
function";
getch();
}
fun function
contains throw
In try instead of writing throw
fun function is calling throw
11.
CATCHING MECHANISM
Acatch block look like a function definition and is of the form
Catch (type arg)
{
// statements for managing exceptions
}
The type indicates which type of exception that catch block handles
The catch statement catches an exception whose type matches with the
type of catch argument. When it is caught the code in the catch block is
executed
Due to mismatched, if an exception is not caught, abnormal program
termination will occur
The catch block is simply skipped if the catch statement does not catch
an exception
MULTIPLE CATCH
Inmultiple catch when an exception is thrown the
exception handlers are searched in order of an
appropriate match
When no match is found the program is terminated
It is possible that arguments of several catch
statement match the type of an exception in such
cases the first handler that matches the exception
is executed
14.
CATCH ALL EXCEPTION
One catch statement can catch all exception
instead of a certain type alone.
This could be done by defining the catch
statements using ellipse as follows
catch (…)
{
// statements for processing
// all exceptions
}