CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Exceptions, Templates, And The Standard Template Library (STL) Chapter 16.
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.
 2006 Pearson Education, Inc. All rights reserved. Exception Handling in C++ CS-2303, C-Term Exception Handling in C++ CS-2303 System Programming.
Exception Handling and Format output
 2000 Prentice Hall, Inc. All rights reserved. Chapter 23 - Exception Handling Outline 23.1Introduction 23.2When Exception Handling Should Be Used 23.3Other.
CSE 332: C++ exceptions Overview of C++ Exceptions Normal program control flow is halted –At the point where an exception is thrown The program call stack.
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.
 Both System.out and System.err are streams—a sequence of bytes.  System.out (the standard output stream) displays output  System.err (the standard.
Copyright © 2012 Pearson Education, Inc. Chapter 16: Exceptions, Templates, and the Standard Template Library (STL)
Exception Handling 1 CISC6795, Spring Introduction 2 Exception – an indication of a problem that occurs during a program’s execution, for examples:
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.
Lesson 16 Exceptions Lesson Exceptions1. Murphy’s Law Anything that can go wrong will go wrong Lesson Exceptions2.
Jerry Lebowitz. Topics  Provides a facility for a systematic object oriented approach to handling runtime errors ◦ Can also handle runtime errors.
C++ Exception Handling
1 CSC241: Object Oriented Programming Lecture No 28.
Understand Error Handling Software Development Fundamentals LESSON 1.4.
E XCEPTION H ANDLING Chapter 11 C S 442: A DVANCED J AVA P ROGRAMMING.
Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  due to poor understanding of the problem and solution.
CS Advanced C++ Exception Handling Topic #5.
© Copyright Eliyahu Brutman Exceptions. © Copyright Eliyahu Brutman Exceptions and Design Patterns - 2 Introduction to Exception Handling Definition:
Exception Handling An Exception is an indication of a problem that occurs during a program’s execution. Exception handling enables the programmer to create.
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
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.
Object Oriented Programming
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 13 - Exception Handling Outline 13.1Introduction 13.2When Exception Handling Should Be Used.
Chapter 12: Exception Handling
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.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
Exception Handling Unit-6. Introduction An exception is a problem that arises during the execution of a program. An exception can occur for many different.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
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.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 23 - Exception Handling Outline 23.1Introduction.
Exception Handling in C++. Outline What exceptions are and when to use them Using try, catch and throw to detect, handle and indicate exceptions, respectively.
Exception Handling Outline 23.1 Introduction
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
CSCI 383 Object-Oriented Programming & Design Lecture 20 Martin van Bommel.
LECTURE LECTURE 14 Exception Handling Textbook p
Exception Handling How to handle the runtime errors.
EE4E. C++ Programming Lecture 6 Advanced Topics. Contents Introduction Introduction Exception handling in C++ Exception handling in C++  An object oriented.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Appendix H Exception Handling: A Deeper Look
Exception handling.
Exception Handling in C++
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.
EXCEPTION HANDLING IN C++
Why exception handling in C++?
Part IX Fundamentals of C and C++ Programming Exception Handling
EXCEPTION HANDLING.
Chapter 14: Exception Handling
Exceptions 1 CMSC 202.
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
Exceptions for safe programming.
CMSC 202 Lesson 20 Exceptions 1.
Presentation transcript:

CMSC 202 Computer Science II for Majors

CMSC 202UMBC Topics Exceptions Exception handling

CMSC 202UMBC Exceptions Exceptions refer to unusual conditions in the program These conditions could be errors that cause program to fail or other conditions that lead to error Exception is an indication of some problem that occurs during the program’s execution Thus they must be detected and handled

CMSC 202UMBC Current mechanism Set a flag / indicator for other code to detect Assert a condition  If condition is false, program fails Issue error message and exit  e.g. “File not found” followed by exit() Handle the error internally  Doesn’t work in real world applications

CMSC 202UMBC Current mechanism Problems with these mechanisms  Intermix program logic with error-handling code  Program becomes difficult to read, modify, maintain and debug C++ provides exception handling mechanism  Separation of error detection and handling

CMSC 202UMBC Exception Exception types  Synchronous errors Divide by zero Out-of-range array subscripts Unsuccessful memory allocations  Asynchronous errors Disk I/O Keyboard interrupt Exception handling in C++ is designed only for synchronous errors

CMSC 202UMBC Exception Handling This mechanism consists of following constructs:  try : this block specifies sequences of code for which exceptions need to be handled  catch : this block specifies sequences of code which handles exceptions that occur in the related try block  throw : this statement causes specific exceptions to be generated

CMSC 202UMBC Exception Handling … cont try blockthrow catch block catch 1 catch 2 Exception throws Exception Handlers No exceptions Next statement following try/catch

CMSC 202UMBC Exception Handling … cont try try { // code which can generate // multiple kinds of exceptions... } If exception occurs in try block, it terminates Program control is transferred to first catch handler after try block

CMSC 202UMBC Exception Handling … cont catch catch (OneTypeOfException& e1) { // code to handle exception } catch (AnotherTypeOfException& e2) { // code to handle exception } catch (... ) { // default handler for exceptions // not caught by other catch blocks }

CMSC 202UMBC Exception Handling … cont Catch block is the error handler The argument in parentheses is called exception parameter  It represents type of exception that catch block can process Appropriate catch handler is located by comparing thrown exception’s type with exception parameter

CMSC 202UMBC Exception Handling … cont If no exceptions occur in try block, all catch blocks are ignored  Control passes to first statement after last catch block If exception occurs in try block with no matching catch handler, function containing exception statement terminates

CMSC 202UMBC Example int main(void) { int numArray[10]; try { for (int i = 0; i <= 10; i++) { TrivialOperation(numArray,i); } // Catch the exception catch (char *str) { cout << "Exception: " << str << endl; } return 0; } void TrivialOperation(int *numArray, int index) { if(index > 9) throw "Out of range exception"; numArray[index] = index * 2; }

CMSC 202UMBC Stack unwinding Program stack holds all local variables declared in the function as it executes If exception is thrown but not caught in particular scope  Function-call stack is unwound  Attempt is made to catch exception in outer try/catch block Function in which exception is not caught terminates, control returns to statement that originally invoked that function

CMSC 202UMBC Example int main() { int x; try { x = FunctionA(-9); } catch (ExceptionType1& e1) { cout << "Exception: Value < 0" << endl; } catch (ExceptionType2& e2) { cout 100" << endl; } return 0; }

CMSC 202UMBC Example … cont int FunctionA(int val1) { int val2; val2 = FunctionB(val1); return 2*val2; } int FunctionB(int num) { if (num < 0) { throw ExceptionType1(); } else if (num > 100) { throw ExceptionType2(); } Assume that ExceptionType1() and ExceptionType1() have already been defined

CMSC 202UMBC Stack unwinding … cont main() => FunctionA() => FunctionB() Exception of type ExceptionType1 is thrown in FunctionB as value passed is less than 0 Exception not caught in FunctionB, hence unwind stack (i.e. terminate function) Control now passes to FunctionA Again no catch for exception in FunctionA, further unwind stack destroying all local variables on stack Control now passes to main, where exception is caught

CMSC 202UMBC Exercise What happens when main also doesn’t catches exception in previous example ? How to handle exceptions during memory allocation when new fails ? Exercise page 806