[go: up one dir, main page]

0% found this document useful (0 votes)
49 views15 pages

Fdocuments - in Exception Handling in C 58bcee0564d59

This 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 cause exceptions is placed in a try block. When an exception occurs, it is thrown using throw. Catch blocks are used to handle the thrown exceptions. Exceptions can be thrown from functions using throw. Multiple catch blocks allow handling different exception types in order. A catch-all block using ellipses (...) can handle any exception.

Uploaded by

Shrikant Bhatade
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)
49 views15 pages

Fdocuments - in Exception Handling in C 58bcee0564d59

This 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 cause exceptions is placed in a try block. When an exception occurs, it is thrown using throw. Catch blocks are used to handle the thrown exceptions. Exceptions can be thrown from functions using throw. Multiple catch blocks allow handling different exception types in order. A catch-all block using ellipses (...) can handle any exception.

Uploaded by

Shrikant Bhatade
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/ 15

G. H.

RAISONI COLLEGE 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)


WHAT IS AN EXCEPTION ?
 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.
COMMON FAILURES
 new not 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.
Try , Catch , Throw
 In Exception handling 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.
RELATING EXCEPTION HANDLING WITH
REAL LIFE
try throw

If a Smartphone
fails the test

catch

to n
d
sen ctio
Re odu
pr
IMPORTANT POINTS
 When a 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;
}
TRY BLOCK
 In an 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)
{
}
#include<iostream>
using namespace std;
main()
{
int a,b;
cout<<"enter values of a and b\n";
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;
}
THROWING MECHANISM
 An exception 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;
}
CALL THROW USING FUNCTION
#include<iostream>
using namespace std;
void fun() fun function
contains throw
{
throw 3;
}
int main()
{
int i=3;
try{
if (i=3)
In try instead of writing throw
fun();
fun function is calling throw
}
catch(int e){
cout<<"\n exception no:"<<e;
}
cout<<"\n throw successfully called by
function";
getch();
}
CATCHING MECHANISM

 A catch 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 STATEMENT
 Syntax for multiple catch statement
MULTIPLE CATCH
 In multiple 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
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
}

You might also like