exception handling - wordpress.com€¦ · 22/06/2016  · exception handling fundamentals..try...

28
Exception Handling

Upload: others

Post on 02-Jun-2020

12 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Exception Handling - WordPress.com€¦ · 22/06/2016  · Exception Handling Fundamentals..try •When an exception is thrown, the remaining code in the try block is skipped, just

Exception Handling

Page 2: Exception Handling - WordPress.com€¦ · 22/06/2016  · Exception Handling Fundamentals..try •When an exception is thrown, the remaining code in the try block is skipped, just

• Introduction : Fundamental

• Syntax for Exception Handling code : try

• catch

• throw

• Multiple exceptions

• Exception with arguments

Page 3: Exception Handling - WordPress.com€¦ · 22/06/2016  · Exception Handling Fundamentals..try •When an exception is thrown, the remaining code in the try block is skipped, just

Introduction

• Exception: “An abnormal condition that arises in a code sequence at run time”.

• An exception is a run-time error.

• Exception handling allows us to manage

run-time errors in an orderly fashion.

Page 4: Exception Handling - WordPress.com€¦ · 22/06/2016  · Exception Handling Fundamentals..try •When an exception is thrown, the remaining code in the try block is skipped, just

#include <iostream> using namespace std; int main() { double a, b, c; // Request two numbers from the user cout << "Please provide two numbers\n"; cout << "First Number: "; cin >> a; cout << "Second Number: "; cin >> b; // Multiply the numbers and display the result c = a * b; cout << "\n" << a << " * " << b << " = " << c << "\n\n"; return 0; }

Introduction

Page 5: Exception Handling - WordPress.com€¦ · 22/06/2016  · Exception Handling Fundamentals..try •When an exception is thrown, the remaining code in the try block is skipped, just

• When it comes up, the user is asked to simply type two numbers; the program would use them to perform a multiplication and display the result.

• Imagine that a user, decides to type the name of a country or somebody’s telephone number as one of the requested values.

• Since a program such as this one is not prepared to multiply two strings or one number to a string it would not know what to do.

Introduction

Page 6: Exception Handling - WordPress.com€¦ · 22/06/2016  · Exception Handling Fundamentals..try •When an exception is thrown, the remaining code in the try block is skipped, just

• The only alternative the compiler would have is to send the problem to the operating system, hoping that the OS would know what to do.

• Whenever the compiler is handed a task, it would try to perform the assignment. If it can’t perform the assignment, for any reason it is not prepared for, it would throw an error.

• As a programmer, if you can anticipate the type of error that could occur in your program, you can catch the error yourself and deal with it by telling the compiler what to do when this type of error occurs.

Introduction

Page 7: Exception Handling - WordPress.com€¦ · 22/06/2016  · Exception Handling Fundamentals..try •When an exception is thrown, the remaining code in the try block is skipped, just

• An exception is a situation that would be unusual for the program that is being processed.

• An error result or an unpredictable behavior on your program not caused by the operating system but that occurs in your program is called an exception.

• The ability to deal with a program’s eventual abnormal behavior is called exception handling.

Introduction

Page 8: Exception Handling - WordPress.com€¦ · 22/06/2016  · Exception Handling Fundamentals..try •When an exception is thrown, the remaining code in the try block is skipped, just

Introduction

• Using exception handling, our program can automatically invoke an error-handling routine when an error occurs.

• C++ exception handling is built upon three keywords: try, catch, and throw.

Page 9: Exception Handling - WordPress.com€¦ · 22/06/2016  · Exception Handling Fundamentals..try •When an exception is thrown, the remaining code in the try block is skipped, just

9

The Basics

• try identifies a code block where exception can occur

• throw causes an exception to be raised (thrown)

• catch identifies a code block where the exception will be handled (caught)

try

{

throw an exception

}

catch the exception

Page 10: Exception Handling - WordPress.com€¦ · 22/06/2016  · Exception Handling Fundamentals..try •When an exception is thrown, the remaining code in the try block is skipped, just

Exception Handling Fundamentals ..try

• Error prone program statements that we may want to monitor for generation of exceptions are contained in a try block.

• Syntax:

try {

// try block

}

Page 11: Exception Handling - WordPress.com€¦ · 22/06/2016  · Exception Handling Fundamentals..try •When an exception is thrown, the remaining code in the try block is skipped, just

Exception Handling Fundamentals..try

• When an exception is thrown, the remaining code in the try block is skipped, just as in the case of the return statement in a function, and every auto object created after the try block is entered, is destroyed automatically

• The thrown object is caught by the catch block where the execution continues

• Then, the execution continues with the next statement after the catch block

Page 12: Exception Handling - WordPress.com€¦ · 22/06/2016  · Exception Handling Fundamentals..try •When an exception is thrown, the remaining code in the try block is skipped, just

Exception Handling Fundamentals …throw

• If an exception (i.e., an error) occurs within the try block, then that exception is thrown using throw.

• Syntax:

throw exception;

• If an exception is to be caught, then throw must be executed either from within a try block or from any function called from within the try block (directly or indirectly).

Page 13: Exception Handling - WordPress.com€¦ · 22/06/2016  · Exception Handling Fundamentals..try •When an exception is thrown, the remaining code in the try block is skipped, just

Exception Handling Fundamentals …catch

• The thrown exception is caught, using catch block and processed.

• Syntax:

catch (type argument)

{

// catch block

}

Page 14: Exception Handling - WordPress.com€¦ · 22/06/2016  · Exception Handling Fundamentals..try •When an exception is thrown, the remaining code in the try block is skipped, just

Catching Exceptions

• You must supply at least one catch block for a try block

• Otherwise compiler error (let's see it in ExceptionSample1.cpp). int main()

{ int height; cin >> height; try { if (height > 300) throw "height exceeds maximum"; if (height < 30) throw "height below minimum"; cout << "Person is " <<ToInches(height) << "inches tall" << endl; } //NO CATCH HERE – NOT ALLOWED return 0; }

Page 15: Exception Handling - WordPress.com€¦ · 22/06/2016  · Exception Handling Fundamentals..try •When an exception is thrown, the remaining code in the try block is skipped, just

Catching Exceptions

• Catch blocks must immediately follow the try block without any program code between them.

– Otherwise, compiler error (let's see it in ExceptionSample1.cpp).

int main() { int height; cin >> height; try { if (height > 300) throw "height exceeds maximum"; if (height < 30) throw "height below minimum"; cout << "Person is " <<ToInches(height) << "inches tall" << endl; } cout << "Wassup"; //no statements are allowed between try and catch catch(const char msg[]) { cout << "Exception occured: "<< msg << endl; } cout << "Program Stops " << endl; return 0; }

Page 16: Exception Handling - WordPress.com€¦ · 22/06/2016  · Exception Handling Fundamentals..try •When an exception is thrown, the remaining code in the try block is skipped, just

try { }

Program statements requires monitor for exceptions

catch( type argument ) { }

Program statements

handles for Exception

Page 17: Exception Handling - WordPress.com€¦ · 22/06/2016  · Exception Handling Fundamentals..try •When an exception is thrown, the remaining code in the try block is skipped, just

try { }

Program statements requires monitor for exceptions

catch( type1 argument ) { }

Program statements

handles for Exception

catch( type2 argument ) { }

Program statements

handles for Exception

catch( typen argument ) { }

Program statements

handles for Exception

Page 18: Exception Handling - WordPress.com€¦ · 22/06/2016  · Exception Handling Fundamentals..try •When an exception is thrown, the remaining code in the try block is skipped, just

Using try & catch

try { }

int d = 0; int a = 30 / d;

catch(int e )

{ }

printf("Division by zero.");

throws

Arithmetic Exception

Page 19: Exception Handling - WordPress.com€¦ · 22/06/2016  · Exception Handling Fundamentals..try •When an exception is thrown, the remaining code in the try block is skipped, just

Combined with the try block, the syntax of an exception would be:

try {

………..

throw exception;

}

catch(Argument)

{

// Catch the exception

}

Syntax for Exception Handling Code : try-catch

Page 20: Exception Handling - WordPress.com€¦ · 22/06/2016  · Exception Handling Fundamentals..try •When an exception is thrown, the remaining code in the try block is skipped, just

NOTE:

Throwing an unhandled exception causes the standard library function terminate() to be invoked. By default, terminate() calls abort() to stop your program.

Exception Handling Fundamentals

Page 21: Exception Handling - WordPress.com€¦ · 22/06/2016  · Exception Handling Fundamentals..try •When an exception is thrown, the remaining code in the try block is skipped, just

#include <iostream>

using namespace std;

int main()

{

int StudentAge;

cout << “Enter Student Age: ";

cin >> StudentAge;

try {

if(StudentAge < 0)

throw;

cout << "\nStudent Age: " << StudentAge << "\n\n";

}

Code : try- catch-throw

Page 22: Exception Handling - WordPress.com€¦ · 22/06/2016  · Exception Handling Fundamentals..try •When an exception is thrown, the remaining code in the try block is skipped, just

catch(...)

{ }

cout << "\n";

return 0;

}

Code : try- catch-throw catch (...) is used to

catch any unhandled

exception

Page 23: Exception Handling - WordPress.com€¦ · 22/06/2016  · Exception Handling Fundamentals..try •When an exception is thrown, the remaining code in the try block is skipped, just

• If positive integer is given as input

Output : Enter Student Age: 1

Student Age: 1

• If negative integer is given as input

Output : Enter Student Age: -1

Program will give an abnormal termination error

Output

Page 24: Exception Handling - WordPress.com€¦ · 22/06/2016  · Exception Handling Fundamentals..try •When an exception is thrown, the remaining code in the try block is skipped, just

#include <iostream>

using namespace std;

int main()

{

double Number1, Number2, Result;

// Request two numbers from the user

cout << "Please provide two numbers\n";

Code : try- catch-throw

Page 25: Exception Handling - WordPress.com€¦ · 22/06/2016  · Exception Handling Fundamentals..try •When an exception is thrown, the remaining code in the try block is skipped, just

try {

cout << "First Number: ";

cin >> Number1;

cout << "Second Number: ";

cin >> Number2;

if( Number2 == 0 )

throw (Number2);

// Perform a division and display the result

Result = Number1 / Number2;

cout << "\n" << Number1 << " / " << Number2 << " = " << Result << "\n\n";

}

Code : try- catch-throw

Page 26: Exception Handling - WordPress.com€¦ · 22/06/2016  · Exception Handling Fundamentals..try •When an exception is thrown, the remaining code in the try block is skipped, just

catch(int i)

{

cout<<“Exception caught : x = “<<x<<“\n”;

}

return 0;

}

Code : try- catch-throw

Page 27: Exception Handling - WordPress.com€¦ · 22/06/2016  · Exception Handling Fundamentals..try •When an exception is thrown, the remaining code in the try block is skipped, just

• Please provide two numbers

First Number: 126.45 Second Number: 5.52

126.45 / 5.52 = 22.9076

Number1 126.45 Number2: 5.52

Result 22.9076

Output

Page 28: Exception Handling - WordPress.com€¦ · 22/06/2016  · Exception Handling Fundamentals..try •When an exception is thrown, the remaining code in the try block is skipped, just

• Please provide two numbers

First Number: 126.45 Second Number: 0

Exception caught : x=0

Output