Lecture 9. 2 Exception  An exception is a unusual, often unpredictable event, detectable by software or hardware, that requires special processing occurring.

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
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.
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
Lesson 16 Exceptions Lesson Exceptions1. Murphy’s Law Anything that can go wrong will go wrong Lesson Exceptions2.
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.
Lecture 27 Exceptions COMP1681 / SE15 Introduction to Programming.
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.
12.1 Exceptions The limitations of traditional methods of exception handling Error conditions are a certainty in programming Programmers make.
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.
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
CPSC 252 Exception Handling Page 1 Exceptions and exception handling Client programmers can make errors using a class attempting to dequeue an item from.
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.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
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.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
Mobility Research Lab mobility.ceng.metu.edu.tr Applied Innovative Interdisciplinary (AI2) Research Lab Short Course on Programming in C/C++
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.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
Exception Handling How to handle the runtime errors.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Exception. Agenda Exception. Handling Exceptions. The finally Clause.
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.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
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
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.
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 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
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.
Exceptions CSCE 121 J. Michael Moore
Part B – Structured Exception Handling
Exceptions 1 CMSC 202.
Object-Oriented Programming (OOP) Lecture No. 43
Ninth step for Learning C++ Programming
Tenth step for Learning C++ Programming
Object-Oriented Programming (OOP) Lecture No. 44
Lecture 9.
CMSC 202 Lesson 20 Exceptions 1.
More C++ Concepts Exception Handling Templates.
Presentation transcript:

Lecture 9

2 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

3 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

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

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

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

Example of a try-catch Statement try { // 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 No statements throw an exception Statement following entire try-catch statement A statement throws an exception Exception Handler Statements to deal with exception are executed Control moves directly to exception handler

Throwing an Exception to be Caught by the Calling Code void Func4() { if ( error ) throw ErrType(); } Normal return void Func3() { try { Func4(); } catch ( ErrType ) { } } Function call 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 }

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

// quotient.cpp -- Quotient program #include #include 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; }

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