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

From Script | Spoken-Tutorial
Jump to: navigation, search

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