Lecture 9.

Slides:



Advertisements
Similar presentations
Chapter 16 Exception Handling. What is Exception Handling? A method of handling errors that informs the user of the problem and prevents the program from.
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Lecture 9. 2 Exception  An exception is a unusual, often unpredictable event, detectable by software or hardware, that requires special processing occurring.
1 Chapter 17-2 Templates and Exceptions Dale/Weems.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.
CSI 3120, Exception handling, page 1 Exception and Event Handling Credits Robert W. Sebesta, Concepts of Programming Languages, 8 th ed., 2007 Dr. Nathalie.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
© Copyright Eliyahu Brutman Exceptions. © Copyright Eliyahu Brutman Exceptions and Design Patterns - 2 Introduction to Exception Handling Definition:
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
Introduction to C++ Templates and Exceptions l C++ Function Templates l C++ Class Templates l Exception and Exception Handler.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Exception Handling in Java Course Lecture Slides 7 th July 2010 “ Admitting.
Exceptions COMPSCI 105 S Principles of Computer Science.
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?? }
1 CSC241: Object Oriented Programming Lecture No 27.
Handling Exceptions in java. Exception handling blocks try { body-code } catch (exception-classname variable-name) { handler-code }
Operator Overloading & Exception Handling TCP1201 OOPDS 1 Lecture 5 1.
Introduction to Exception Handling and Defensive Programming.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
1 An Exception is… An unusual, often unpredictable event, detectable by software or hardware, that requires special processing An exception handler is.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
CSCI 383 Object-Oriented Programming & Design Lecture 20 Martin van Bommel.
1 Chapter 17 Templates and Exceptions Dale/Weems.
1 Chapter 17 Templates and Exceptions Dale/Weems/Headington.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
Exception Handling How to handle the runtime errors.
Introduction to C++ Templates and Exceptions C++ Function Templates C++ Class Templates Exception and Exception Handler.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Exceptions an unusual condition – e.g. division by zero – e.g. file doesn't exist – e.g. illegal type – etc. etc… typically a run-time error – i.e. during.
Lecture 11 Dr. Eng. Ibrahim El-Nahry Exception Handling.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Object-Oriented Programming in C++ Exception.
CSE 1201 Object Oriented Programming
C++ Exceptions.
Exceptions Error Handling and Recovery
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.
CSCE Student presentation LARRY PARKER
Andy Wang Object Oriented Programming in C++ COP 3330
Why exception handling in C++?
Part IX Fundamentals of C and C++ Programming Exception Handling
Exceptions C++ Interlude 3
Exceptions with Functions
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 17 Templates and Exceptions Part 2
Chapter 12 Exception Handling and Text IO
Chapter 12 Exception Handling
Exceptions Problems in a Java program may cause exceptions or errors representing unusual or invalid processing. An exception is an object that defines.
Exception Handling CSCI293 - C# October 3, 2005.
Exception Handling Chapter 9 Edited by JJ.
Exception Handling In Text: Chapter 14.
Exception Handling Oo28.
Exceptions CSCE 121 J. Michael Moore
Part B – Structured Exception Handling
CSC 270 – Survey of Programming Languages
Exceptions 1 CMSC 202.
Exception Handling Imran Rashid CTO at ManiWeber Technologies.
Object-Oriented Programming (OOP) Lecture No. 43
Ninth step for Learning C++ Programming
Tenth step for Learning C++ Programming
Exception and Event Handling
Object-Oriented Programming (OOP) Lecture No. 44
Exception Handling and Event Handling
Exceptions for safe programming.
CMSC 202 Exceptions.
Exception Handling.
CMSC 202 Lesson 20 Exceptions 1.
More C++ Concepts Exception Handling Templates.
Presentation transcript:

Lecture 9

Exception An exception is a unusual, often unpredictable event, detectable by software or hardware, that requires special processing occurring at runtime. In C++, a variable or class object that represents an exceptional event

Handling Exception Without handling, Program crashes Falls into unknown state An exception handler is a section of program code that is designed to execute when a particular exception occurs Resolve the exception Lead to known state, such as exiting the program

Standard Exceptions Exceptions Thrown by the Language new Exceptions Thrown by Standard Library Routines Exceptions Thrown by user code, using throw statement

The throw Statement Throw: to signal the fact that an exception has occurred; also called raise throw Expression ThrowStatement

The try-catch Statement How one part of the program catches and processes the exception that another part of the program throws. TryCatchStatement try Block catch (FormalParameter) FormalParameter DataType VariableName …

Example of a try-catch Statement { // Statements that process personnel data and may throw // exceptions of type int, string, and SalaryError } catch ( int ) // Statements to handle an int exception catch ( string s ) cout << s << endl; // Prints "Invalid customer age" // More statements to handle an age error catch ( SalaryError ) // Statements to handle a salary error

Execution of try-catch statement throws an exception No statements throw an exception Exception Handler Control moves directly to exception handler Statements to deal with exception are executed Statement following entire try-catch statement

Throwing an Exception to be Caught by the Calling Code void Func3() { try Func4(); } catch ( ErrType ) void Func4() { if ( error ) throw ErrType(); } Function call Normal return Return from thrown exception

Practice: Dividing by ZERO Apply what you know: int Quotient(int numer, // The numerator int denom ) // The denominator { if (denom != 0) return numer / denom; else //What to do?? do sth. to avoid program //crash }

A Solution int Quotient(int numer, // The numerator int denom ) // The denominator { if (denom == 0) throw DivByZero(); //throw exception of class DivByZero return numer / denom; }

A Solution // Exception class // quotient.cpp -- Quotient program #include<iostream.h> #include <string.h> int Quotient( int, int ); // Exception class class DivByZero {}; int main() { int numer; // Numerator int denom; // Denominator //read in numerator and denominator while(cin) { try { cout << "Their quotient: " <<Quotient(numer,denom)<<endl; } //exception handler catch ( DivByZero) { cout<<“Denominator can't be 0"<< endl; } // read in numerator and denominator } return 0;

Take Home Message An exception is a unusual, often unpredictable event that requires special processing occurring at runtime Throw Try-catch