Difference between revisions of "Advanced-C++/C2/Exception-Handling/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with ''''Title of script''': Objects and Classes in C++ '''Author: '''Ashwini Patil '''Keywords: Exception handling, try, catch, throw Video tutorial.''' {| style="border-spacing:…')
 
 
Line 1: Line 1:
'''Title of script''': Objects and Classes in C++
+
'''Title of script''': Exception Handling in C++
  
 
'''Author: '''Ashwini Patil
 
'''Author: '''Ashwini Patil
Line 17: Line 17:
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 2
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 2
 +
 +
Learning Objectives
  
  
Line 28: Line 30:
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 3  
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 3  
 +
 +
System requirements
  
  
Line 33: Line 37:
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| To record this tutorial, I am using
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| To record this tutorial, I am using
  
'''Ubuntu OS '''version 11.04
+
'''Ubuntu OS '''version 11.10
  
'''gcc '''and '''g++ compiler '''v. 4.6.1
+
'''g++ compiler '''v. 4.6.1
  
 
|-
 
|-
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 4
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 4
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us start with the introduction to exception handling.
+
  
The response given to the problems occured during the execution of the program.
+
Introduction to Exception
 +
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us start with an introduction to exception
 +
 
 +
An exception is a problem that arises during the execution of a program
 +
 
 +
It is a run-time error that a program may detect
 +
 
 +
|-
 +
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 5
 +
 
 +
Introduction to Exception Handling
 +
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us move on to exception handling
 +
 
 +
The response given to the problems occured during the execution of the program is known as exception handling.
  
 
Exception handling allows a program to continue execution.
 
Exception handling allows a program to continue execution.
Line 50: Line 66:
  
 
|-
 
|-
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 5
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 6
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Types of Exception handling
+
 
 +
Types of Exceptions
 +
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us see the Types of Exceptions
  
 
Try
 
Try
Line 63: Line 81:
 
Then it is Handled using throw.  
 
Then it is Handled using throw.  
  
after this the exception is caught, using catch
+
after this the exception is caught, using catch statement
  
And it is processed.  
+
And then it is processed.  
  
 
|-
 
|-
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 5
+
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Slide 6
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| The syntax for try catch is:
+
 
 +
Syntax
 +
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| The syntax for try catch and throw is:
 +
 
 +
Throw:
  
 
try {  
 
try {  
Line 89: Line 111:
 
}  
 
}  
  
As we discussed earlier,
+
The try block and the catch block, here we pass argument
  
When an exception is thrown it is cought by the catch statement.
+
The throw statement can be written inside the try block as well
  
We can have more than one try-catch blocks.
+
We can have more than try, catch blocks.
  
 
|-
 
|-
Line 107: Line 129:
 
In this program we will solve the divide by zero error using exception handling.
 
In this program we will solve the divide by zero error using exception handling.
  
Let me explain the code now.
+
Let us go through the code
  
 
|-
 
|-
Line 117: Line 139:
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This is our '''header file '''as''' iostream.'''
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This is our '''header file '''as''' iostream.'''
  
We have used '''std namespace.'''
+
Here we are using the '''std namespace.'''
  
 
|-
 
|-
Line 141: Line 163:
 
We check whether''' b ==0.'''
 
We check whether''' b ==0.'''
  
If true, we throw an exception.
+
If true, we throw an exception division by zero condition
  
 
The function returns division of''' a '''and''' b'''
 
The function returns division of''' a '''and''' b'''
Line 164: Line 186:
  
 
'''cin>>x>>y;'''
 
'''cin>>x>>y;'''
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here we have declared three variables as '''x,''' '''y '''and''' z.'''
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| In this we have declared three variables as '''x,''' '''y '''and''' z.'''
  
  
Then we accept the values of''' x '''and''' y '''from user.
+
Here we accept the values of''' x '''and''' y.'''
  
  
Line 186: Line 208:
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This is our '''try block'''
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This is our '''try block'''
  
Inside it we use call the function''' division.'''
+
Here we have called the function''' division.'''
  
It is stored in''' z.'''
+
And stored the result in '''z.'''
  
 
Then we print the value of''' z.'''
 
Then we print the value of''' z.'''
Line 201: Line 223:
  
  
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Here is our''' catch block.'''
+
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This is our''' catch block.'''
  
We pass an''' argument msg '''as a''' character constant.'''
+
In this we pass an''' argument msg '''as a''' character constant.'''
  
 
|-
 
|-
Line 217: Line 239:
  
 
'''return 0;'''
 
'''return 0;'''
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This is our return statement.
+
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| And this is our return statement.
 
+
|-
+
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Click on save
+
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Now Click on '''Save'''
+
  
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"|  
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:0.25pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"|  
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us execute
+
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us execute the program
  
 
|-
 
|-
Line 231: Line 249:
  
 
'''Ctrl, Alt and T '''keys simultaneously
 
'''Ctrl, Alt and T '''keys simultaneously
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| open the terminal by pressing '''Ctrl, Alt and T '''keys simultaneously
+
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| open the terminal by pressing '''Ctrl, Alt and T '''keys simultaneously on your keyboard
  
 
|-
 
|-
Line 243: Line 261:
  
 
'''./exp'''
 
'''./exp'''
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| To compile the program type  
+
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| To compile type  
  
'''g++ exception.cpp -o exp'''
+
'''g++ exception.cpp -o ex'''
  
 
To execute
 
To execute
Line 251: Line 269:
 
Type
 
Type
  
'''./exp'''
+
'''./ex'''
  
 
|-
 
|-
Line 261: Line 279:
 
'''Enter the value of x and y:'''
 
'''Enter the value of x and y:'''
  
'''I will first enter as 20 and 2'''
+
'''I will first enter as 3 and 0'''
  
 
'''The output is displayed as:'''
 
'''The output is displayed as:'''
  
'''10.'''
+
'''Division by zero condition'''
  
'''Now let us execute again'''
+
'''Let us compile again'''
 +
 
 +
'''Press the up arrow key twice '''
 +
 
 +
'''Press enter, again press the up arrow key twice'''
 +
 
 +
'''Enter value of x and y'''
 +
 
 +
'''i will give as 8 and 2'''
 +
 
 +
'''The output is 4'''
  
'''This time i will enter the values as 30 and 0'''
 
  
'''The output is displayed as:'''
 
  
'''Divide by zero condition.'''
 
  
 
|-
 
|-
Line 283: Line 308:
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This brings us to the end of this tutorial.
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| This brings us to the end of this tutorial.
  
Let us come back to the slides
+
Let us come back to our slides
  
 
|-
 
|-
Line 289: Line 314:
  
 
Summary
 
Summary
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| In this tutorial, we have seen,
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Let us summarize, In this tutorial, we have seen,
  
 
Exception Handling
 
Exception Handling
Line 297: Line 322:
 
Catch
 
Catch
  
Throw
+
Throw blocks
  
 
|-
 
|-
Line 340: Line 365:
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Spoken Tutorial Project is a part of the Talk to a Teacher project
 
| style="background-color:#ffffff;border-top:0.25pt solid #c0c0c0;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.018cm;padding-right:0.018cm;"| Spoken Tutorial Project is a part of the Talk to a Teacher project
  
It is supported by the National Mission on Education through ICT, MHRD, Government of Indiaabout:startpage
+
It is supported by the National Mission on Education through ICT, MHRD, Government of India
  
 
More information on this Mission is available at: [http://spoken-tutorial.org/NMEICT-Intro http://spoken-tutorial.org\NMEICT-Intro]
 
More information on this Mission is available at: [http://spoken-tutorial.org/NMEICT-Intro http://spoken-tutorial.org\NMEICT-Intro]

Latest revision as of 12:48, 17 July 2013

Title of script: Exception Handling in C++

Author: Ashwini Patil

Keywords: Exception handling, try, catch, throw Video tutorial.


Visual Cue
Narration
Slide 1 Welcome to the spoken tutorial on Exception Handling in C++.
Slide 2

Learning Objectives


In this tutorial we will learn,

Exception Handling.

We will do this with the help of an example.

Slide 3

System requirements


To record this tutorial, I am using

Ubuntu OS version 11.10

g++ compiler v. 4.6.1

Slide 4

Introduction to Exception

Let us start with an introduction to exception

An exception is a problem that arises during the execution of a program

It is a run-time error that a program may detect

Slide 5

Introduction to Exception Handling

Let us move on to exception handling

The response given to the problems occured during the execution of the program is known as exception handling.

Exception handling allows a program to continue execution.

Helps Identifying the problem.

Terminates the program in a controlled manner.

Slide 6

Types of Exceptions

Let us see the Types of Exceptions

Try

Catch

Throw

We place the error prone code inside a try block

Then it is Handled using throw.

after this the exception is caught, using catch statement

And then it is processed.

Slide 6

Syntax

The syntax for try catch and throw is:

Throw:

try {

// try block

}

catch (type1 arg) {

// catch block

}

catch (type2 arg) {

// catch block

}

The try block and the catch block, here we pass argument

The throw statement can be written inside the try block as well

We can have more than try, catch blocks.

Now let us see an example on exception handling.

I have the code, i will explain it to you.

our filename is exception.cpp

In this program we will solve the divide by zero error using exception handling.

Let us go through the code

Highlight

#include <iostream>

using namespace std;

This is our header file as iostream.

Here we are using the std namespace.

Highlight

double division(int a, int b)

{

if( b == 0 )

{

throw "Division by zero condition!";

}

return (a/b);

}

Here we have function division with argument as int a and int b.

We check whether b ==0.

If true, we throw an exception division by zero condition

The function returns division of a and b



Highlight

int main()

This is our main function.
Highlight

int x,y;

double z;

cout<<"Enter value of x and y\n";

cin>>x>>y;

In this we have declared three variables as x, y and z.


Here we accept the values of x and y.



Highlight

try

{

z = division(x, y);

cout << z<<"\n";

}

This is our try block

Here we have called the function division.

And stored the result in z.

Then we print the value of z.

Highlight

catch (const char* msg)

{


This is our catch block.

In this we pass an argument msg as a character constant.

Highlight

cout << msg << endl;

}

Then we print the msg.
Highlight

return 0;

And this is our return statement.
Let us execute the program
Open the terminal

Ctrl, Alt and T keys simultaneously

open the terminal by pressing Ctrl, Alt and T keys simultaneously on your keyboard
Type

g++ exception.cpp -o exp

To execute

Type

./exp

To compile type

g++ exception.cpp -o ex

To execute

Type

./ex

Highlight

Output

Here we see,

Enter the value of x and y:

I will first enter as 3 and 0

The output is displayed as:

Division by zero condition

Let us compile again

Press the up arrow key twice

Press enter, again press the up arrow key twice

Enter value of x and y

i will give as 8 and 2

The output is 4



This is how the try catch block works.
Switch back to the slides This brings us to the end of this tutorial.

Let us come back to our slides

Slide 7

Summary

Let us summarize, In this tutorial, we have seen,

Exception Handling

Try

Catch

Throw blocks

Slide 6

Assignment

As an assignment

Display the age of employess.

Throw an exception to check that the age is not less than 15.

Slide 8

About the Spoken Tutorial Project

Watch the video available at the link shown

It summarizes the Spoken Tutorial project

If you do not have good bandwidth, you can download and watch it

Slide 9

Spoken Tutorial Workshops

The Spoken Tutorial Project Team

Conducts workshops using spoken tutorials

Gives certificates to those who pass an online test

For more details, please write to,

contact@spoken-tutorial.org

Slide Number 10


Acknowledgement

Spoken Tutorial Project is a part of the Talk to a Teacher project

It is supported by the National Mission on Education through ICT, MHRD, Government of India

More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro

This is Ashwini Patil from IIT Bombay signing off

Thank You for joining.

Contributors and Content Editors

Ashwini