Advanced-C++/C2/Exception-Handling/English

From Script | Spoken-Tutorial
Revision as of 13:25, 6 May 2013 by Ashwini (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Title of script: Objects and Classes 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


In this tutorial we will learn,

Exception Handling.

We will do this with the help of an example.

Slide 3


To record this tutorial, I am using

Ubuntu OS version 11.04

gcc and g++ compiler v. 4.6.1

Slide 4 Let us start with the introduction to exception handling.

The response given to the problems occured during the execution of the program.

Exception handling allows a program to continue execution.

Helps Identifying the problem.

Terminates the program in a controlled manner.

Slide 5 Types of Exception handling

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

And it is processed.

Slide 5 The syntax for try catch is:

try {

// try block

}

catch (type1 arg) {

// catch block

}

catch (type2 arg) {

// catch block

}

As we discussed earlier,

When an exception is thrown it is cought by the catch statement.

We can have more than one 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 me explain the code now.

Highlight

#include <iostream>

using namespace std;

This is our header file as iostream.

We have used 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.

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;

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


Then we accept the values of x and y from user.



Highlight

try

{

z = division(x, y);

cout << z<<"\n";

}

This is our try block

Inside it we use call the function division.

It is stored in z.

Then we print the value of z.

Highlight

catch (const char* msg)

{


Here is our catch block.

We pass an argument msg as a character constant.

Highlight

cout << msg << endl;

}

Then we print the msg.
Highlight

return 0;

This is our return statement.
Click on save Now Click on Save
Let us execute
Open the terminal

Ctrl, Alt and T keys simultaneously

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

g++ exception.cpp -o exp

To execute

Type

./exp

To compile the program type

g++ exception.cpp -o exp

To execute

Type

./exp

Highlight

Output

Here we see,

Enter the value of x and y:

I will first enter as 20 and 2

The output is displayed as:

10.

Now let us execute again

This time i will enter the values as 30 and 0

The output is displayed as:

Divide by zero condition.

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 the slides

Slide 7

Summary

In this tutorial, we have seen,

Exception Handling

Try

Catch

Throw

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 Indiaabout:startpage

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