Exception Handling How to handle the runtime errors.

Slides:



Advertisements
Similar presentations
Pearson Education, Inc. All rights reserved. 1.. Exception Handling.
Advertisements

 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Exception Handling Outline 13.1 Introduction 13.2 Exception-Handling Overview 13.3 Other.
Exception Handling. Introduction Errors can be dealt with at place error occurs –Easy to see if proper error checking implemented –Harder to read application.
Lecture 9. 2 Exception  An exception is a unusual, often unpredictable event, detectable by software or hardware, that requires special processing occurring.
Exception Handling Introduction Try Throw Catch. Exception Handling Use exception handling to write programs that are –Clearer –More robust –More fault-tolerant.
Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer.
1 Chapter 17-2 Templates and Exceptions Dale/Weems.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 16 Exception Handling.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 23 - Exception Handling Outline 23.1Introduction 23.2When Exception Handling Should Be Used 23.3Other.
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
Objectives In this chapter you will: Learn what an exception is Learn how to handle exceptions within a program See how a try / catch block is used to.
Dale Roberts Exception Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  due to poor understanding of the problem and solution.
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
Exceptions Objectives At the conclusion of this lesson, students should be able to Explain the need for exceptions Correctly write programs that use.
Exceptions COMPSCI 105 S Principles of Computer Science.
Dale Roberts Exception Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
Dr. Abraham. Exception Any problem that VB or OS could not handle Robust program A program that performs well not only under ordinary conditions but also.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 13 - Exception Handling Outline 13.1Introduction 13.2When Exception Handling Should Be Used.
Handling ErrorstMyn1 Handling Errors Up to this point we haven't worried much about errors or exceptions. First, let's distinguish between errors and exceptions.
C++ Exceptions STL Vector. Example int Quotient (int numer, int denom} { if (denom != 0) return (numer/denom); else //What to do?? }
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
1 CSC241: Object Oriented Programming Lecture No 27.
Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  Due to poor understanding of the problem and solution.
Operator Overloading & Exception Handling TCP1201 OOPDS 1 Lecture 5 1.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
Exceptions and Program Correctness based on the original work by Dr. Roger deBry Version 1.1.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
Exception Handling Outline 23.1 Introduction
Mobility Research Lab mobility.ceng.metu.edu.tr Applied Innovative Interdisciplinary (AI2) Research Lab Short Course on Programming in C/C++
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
LECTURE LECTURE 14 Exception Handling Textbook p
Chapter 8-Exception Handling/ Robust Programming.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
EE4E. C++ Programming Lecture 6 Advanced Topics. Contents Introduction Introduction Exception handling in C++ Exception handling in C++  An object oriented.
Introduction to Programming Lesson 1. Algorithms Algorithm refers to a method for solving problems. Common techniques for representing an algorithms:
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Object-Oriented Programming in C++ Exception.
Exception handling.
IS 0020 Program Design and Software Tools
Exceptions Error Handling and Recovery
Chapter 16 Exception Handling
Exceptions Exceptions are used to signal that an unexpected event has happened in a program C++ will generate exceptions for some errors in the program.
Exceptions.
Andy Wang Object Oriented Programming in C++ COP 3330
Why exception handling in C++?
Part IX Fundamentals of C and C++ Programming Exception Handling
EXCEPTION HANDLING.
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 17 Templates and Exceptions Part 2
Chapter 12 Exception Handling and Text IO
Exception Handling Chapter 9 Edited by JJ.
Exceptions CSCE 121 J. Michael Moore
Exceptions 1 CMSC 202.
Object-Oriented Programming (OOP) Lecture No. 43
Introduction to Programming
Object-Oriented Programming (OOP) Lecture No. 44
Department of Computer and Information Science, School of Science, IUPUI Exception Handling Dale Roberts, Lecturer Computer Science, IUPUI
Exception Handling.
CMSC 202 Lesson 20 Exceptions 1.
Presentation transcript:

Exception Handling How to handle the runtime errors

1. What is Exception? Exception means Errors 2. When does the exception occur ? At 2 Places I) at compile time II) at runtime Corrected by the programmer The compiler will not be able to find them or correct them. Syntax Errors LogicalErrors&FaultyInput

This makes programs more robust This makes programs more robust 1. Your Program does not terminate when a wrong input is given 2. Your program itself will identify what type of error has occurred and it will inform the user to correct himself What is the use of exception Handling? What kinds of exceptions does C++ can handle? C++ has exception handling features to handle all kinds of errors Some Situations where an exception may occur - Next Slide

Some of the Situations when an exception may occur class Student {...} int main () {Student *aStudent; aStudent = new Student (...);... } Division by zero float x, y; …. y = 0.0; float result = x/y Index out of range int array [10]; for (int i=0; i<=10; i++) array [i] = something; Cannot divide by zero Array index are from 0 to 9. There is no such element with index 10 It might be the case where there is no enough space in memory to dynamically allocate memory spaces to aStudent.

Functions provided by C++ for exception handling 1. We write all the statements which we feel may generate an error during runtime into the try block 2.A try block can be followed by one or more Catch Blocks There are three functions in C++ to handle exceptions, they are 1.try 2. Catch 3.throw What is a try block ?

main ( ) { int a; cin >> a; cout << a; } When to use try block ? If the Input is : 10 Output is : 10 No Problem If the Input is : A Sample Run 1 Sample Run 2 Program Stops What should I do? main ( ) {int a; try { cin >> a; cout << a; }} Use Try and Catch Blocks in the Program

Catch is used to catch an exception which has occurred within the statements written within the try block What is a Catch Block ? What to write within the catch block? The Code to be executed when an error occurs How many catch blocks to write ? One catch block for one type of exception

How to Use a catch block with a try block ? main ( ) {int a; try { cin >> a; cout << a; } catch( ) { cout<<“Enter Numeric Data”; }} Will catch be executed when a Numeric Data is entered? For Every Catch Block there should be parameter

How to use many catch blocks with one try block ? Try { // statements that may generate exceptions } Catch (type 1 argument) { // error handling code; } Catch (type 2 argument) { // error handling code; } Catch (type 3 argument) { // error handling code; } Which catch block will be instigated? (invoked) The Execution of the catch block depends on the TYPE of the exception generated

What is throw ? It is a built in function to raise or generating an exception by the programmer himself. Syntax: throw (argument); optional throw; What will happen ? So an exception is generated and it will be caught by the catch block.

# include # include int main() int main() {int value1, value2, result; {int value1, value2, result; }// end of main try { cin >> value1; cin >> value2; }// end of try catch ( ) { cout << " just cannot divide by zero"; { cout << " just cannot divide by zero"; }// end of catch }// end of catch if (value2 == 0) { throw ; } result = value1/value2; cout <<"result is :"<< result; If there is an exception, we throw it to a handler We write the code in a try block If there is no exception, we resume the execution

try {cin >> value1;cin >> value2; }// end of try if (value2 == 0) {throw ; {throw ; } result = value1/value2; cout <<"result is :"<< result; if (value1 < 0) {throw (value1); {throw (value1); } catch ( ) {cout << " just cannot divide {cout << " just cannot divide by zero"; by zero"; }// end of catch }// end of catch catch (int v ) {cout << v << "is less than zero, {cout << v << "is less than zero, can’t you see?"; can’t you see?"; }// end of catch }// end of catch int main() { int value1, value2, result; … return 0; }// end of main

What is the Output ? void main( ) { try { cout << “inside try block”; throw 50; cout << “end”; } catch (int c) { cout << “catch type 1”; } catch (float c) { cout << ‘catch type 2”; } } statement Will this statement be executed?

- What is the output ? # include void Xhandler( int test) { try { if (test ==0) throw 0; if (test ==1) throw ‘a’; if (test ==0) throw ;} catch ( int i) { cout << “first type”;} catch ( char x) { cout << “second type”;} catch ( double y) { cout << “third type”;} } Int main( ) { cout << “start”; Xhandler(0); Xhandler(1); Xhandler(2); cout << “end; }

Example - What is the output ? # include void Xhandler( int test) { try { if (test ==0) throw 0; if (test ==1) throw ‘a’; if (test ==0) throw ;} catch (...) { cout << “Caught an exception”; } Int main( ) { cout << “start”; Xhandler(0); Xhandler(1); Xhandler(2); cout << “end; } Catch all Exceptions

Restricting Exceptions Can restrict the type of the exceptions a function can throw # include # include void Xhandler( int test)throw(int,char,double) try { try { if (test ==0) throw 0; if (test ==1) throw ‘a’; if (test ==0) throw ;} catch ( int i) { cout << “first type”;} catch ( char x) { cout << “second type”;} catch ( double y) { cout << “third type”; } } Int main( ) { cout << “start”; Xhandler(0);Xhandler(1);Xhandler(2); cout << “end;} throw (typelist)

Throwing an exception from within a catch block is called rethrowing. Int main( ) { try { xhandler( ); } catch(const char *) { cout << “caught char * inside main”; } Void xhandler ( ) { try { throw “hello”;} catch (const char *) { cout << “insidexhandler”; throw; } What is Re throwing ?

#include class Test { public: Test(){}; ~Test(){}; void ShowReason( ) { cout << " in Test class."; } }; int main( ) { try { throw TEST( ); // statements } catch (TEST E) {cout << “in main”; E.showreason( ); }} How to throw class as exceptions?

Computer Programming II 19 #include #include class DivideByZeroException { public: DivideByZeroException() : message( "attempted to divide by zero" ) { } DivideByZeroException() : message( "attempted to divide by zero" ) { } const char *what() const { return message; } const char *what() const { return message; }private: const char *message; const char *message;}; double quotient( int numerator, int denominator ) { if ( denominator == 0 ) if ( denominator == 0 ) throw DivideByZeroException(); throw DivideByZeroException(); return static_cast ( numerator ) / denominator; return static_cast ( numerator ) / denominator;} Example 2

Computer Programming II 20 int main() { int number1, number2; double result; cout << "Enter two integers (end-of-file to end): "; while ( cin >> number1 >> number2 ) { try { result = quotient( number1, number2 ); cout << "The quotient is: " << result << endl; } catch ( DivideByZeroException ex ) { // exception handler cout << "Exception occurred: " << ex.what() << '\n'; } cout << "\nEnter two integers (end-of-file to end): "; } cout << endl; return 0; // terminate normally }