CSC 270 – Survey of Programming Languages C++ Lecture 6 – Exceptions.

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout
Advertisements

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.
Lecture 9. 2 Exception  An exception is a unusual, often unpredictable event, detectable by software or hardware, that requires special processing occurring.
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.
Exceptions OO Software Design and Construction Computer Science Dept Va Tech January 2002 ©2002 McQuain WD & Keller BJ 1 Exceptions exceptiona program.
Exception Handling By: Thomas Fasciano. What is an Exception?  An error condition that occurs during the execution of a Java Program.
1 Problem Solving with C++ The Object of Programming Walter Savitch Chapter 16 Exception Handling Slides by David B. Teague, Western Carolina University,
Exception Handling. Introduction One benefit of C++ over C is its exception handling system. An exception is a situation in which a program has an unexpected.
Computer Science 1620 Function Scope & Global Variables.
Exceptions Objectives At the conclusion of this lesson, students should be able to Explain the need for exceptions Correctly write programs that use.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
Debugging Logic Errors CPS120 Introduction to Computer Science Lecture 6.
Introduction to Computer Programming Error Handling.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 9 : Exception Handling King Fahd University of Petroleum & Minerals College of Computer.
CPSC 252 Exception Handling Page 1 Exceptions and exception handling Client programmers can make errors using a class attempting to dequeue an item from.
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.
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 }
1 CS 1430: Programming in C++. 2 Literal Values Literal values of int Literal values of float
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.
HANDLING EXCEPTIONS Chapter 9. Outline  Learn about the limitations of traditional error-handling methods  Throw exceptions  Use try blocks  Catch.
Input a number #include using namespace std; int main() { int num; cout num; return 0; }
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.
Mobility Research Lab mobility.ceng.metu.edu.tr Applied Innovative Interdisciplinary (AI2) Research Lab Short Course on Programming in C/C++
LECTURE LECTURE 14 Exception Handling Textbook p
Chapter 61 Example : For … To… Step For index = 0 To n Step s lstValues.Items.Add(index) Next Control variable Start value Stop value Amount to add to.
1 Reference Variables Chapter 8 Page Reference Variables Safer version of C/C++ pointer. "Refers to" a variable. Like a pointer. Effectively.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
C++ Namespaces, Exceptions CSci 588: Data Structures, Algorithms and Software Design All material not from online sources copyright © Travis Desell, 2011.
Exception Handling How to handle the runtime errors.
1 17/4/1435 h Monday Lecture 3 The Parts of a C++ Program.
01/05/100 1 Loops/Iteration Used to repeat an action Must have a STOP condition Three flavors - for, while, do/while.
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.
2.4 Exceptions n Detects try { //code that may raise an exception and/or set some condition if (condition) throw exceptionName; //Freq. A string } n Handles.
C ++ MULTIPLE CHOICE QUESTION
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.
CS 2704 Object Oriented Software Design and Construction
Andy Wang Object Oriented Programming in C++ COP 3330
Why exception handling in C++?
Part IX Fundamentals of C and C++ Programming Exception Handling
Object Oriented Programming COP3330 / CGS5409
Reserved Words.
Exceptions with Functions
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 17 Templates and Exceptions Part 2
Starting Out with C++: From Control Structures through Objects
Exceptions CSCE 121 J. Michael Moore
CSC 270 – Survey of Programming Languages
Exceptions 1 CMSC 202.
By Hector M Lugo-Cordero September 3, 2008
Object-Oriented Programming (OOP) Lecture No. 43
Object-Oriented Programming (OOP) Lecture No. 44
Lecture 9.
Exceptions for safe programming.
CMSC 202 Exceptions.
ENERGY 211 / CME 211 Lecture 24 November 14, 2008.
Basic Exception Handling
CMSC 202 Lesson 20 Exceptions 1.
Presentation transcript:

CSC 270 – Survey of Programming Languages C++ Lecture 6 – Exceptions

Step 1: Create an error class Create a small class named as the error type If you inherit from runtime_error it will inherit a what method – Found in Can use an error class that exists instead

ExceptionDemo.cpp creation of the Error classes #include using namespace std; classNegativeNumber{ public: NegativeNumber(void) {} NegativeNumber(string theMessage) : message(theMessage) {} string getMessage() const {return message; } private: string message; }; class DivideByZero {};

Step 2: Throw when you find error Use the throw command Follow it by an object of an error class Throw will – Stop executing the method it is in – give that object back to the calling program If it is not caught, it will crash the program.

intmain(void) { intpencils, erasers; doubleppe;//pencils per eraser try{ cout << "How many pencils do you" << " have?\n"; cin >> pencils; if (pencils < 0) throw NegativeNumber("pencils");

Catch the error Surround the call to the method that may throw an error with a try { } After the try, add a catch block – Catch ( what you want to catch and its var name){ } Inside the catch block, you can access the variables inside the error object

cout << "How many erasers do you" << " have?\n"; cin >> erasers; if (erasers < 0) throw NegativeNumber("erasers"); if (erasers != 0) ppe = pencils / (double) erasers; else throw DivideByZero(); cout << "Each eraser must last through " << ppe << " pencils." << endl; }

catch(NegativeNumber e){ cout << "Cannot have a negative number of " << e.getMessage() << endl; } catch(DivideByZero){ cout << "Do not make any mistakes!" << endl; } cout << "End of program." << endl; return(0 );