CSC 270 – Survey of Programming Languages

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

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 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.
Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  due to poor understanding of the problem and solution.
Exceptions Objectives At the conclusion of this lesson, students should be able to Explain the need for exceptions Correctly write programs that use.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
Introduction to Computer Programming Error Handling.
C++ Exceptions STL Vector. Example int Quotient (int numer, int denom} { if (denom != 0) return (numer/denom); else //What to do?? }
February 11, 2005 More Pointers Dynamic Memory Allocation.
1 Writing a Good Program 8. Elementary Data Structure.
CSC 270 – Survey of Programming Languages C++ Lecture 6 – Exceptions.
Exception Handling. C++ 2 Outline  Throwing and handling exceptions  Exceptions of different types  The new operator and the exceptions  Re-throwing.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
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.
LECTURE LECTURE 14 Exception Handling Textbook p
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.
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++ Lesson 1.
C ++ MULTIPLE CHOICE QUESTION
IS 0020 Program Design and Software Tools
Exceptions Error Handling and Recovery
Abstract Class Abstract Class is a class which contains atleast one Pure Virtual function in it. Abstract classes are used to provide an Interface for.
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
Command Line Arguments
Why exception handling in C++?
Part IX Fundamentals of C and C++ Programming Exception Handling
Arrays Part-1 Armen Keshishian.
Programming Fundamentals
EXCEPTION HANDLING.
לולאות קרן כליף.
Object Oriented Programming COP3330 / CGS5409
Reserved Words.
Andy Wang Object Oriented Programming in C++ COP 3330
Dynamic Memory Allocation Reference Variables
Learning Objectives What else in C++ Bitwise operator
null, true, and false are also reserved.
Exceptions with Functions
Andy Wang Object Oriented Programming in C++ COP 3330
C++ fundamentals Lecture 1, Chapter 2 – pp /22/2018 Y K Choi.
Chapter 17 Templates and Exceptions Part 2
Chapter 12 Exception Handling and Text IO
Screen output // Definition and use of variables
הרצאה 03 אבני היסוד של תוכנית ב- C
Starting Out with C++: From Control Structures through Objects
Pointers & Functions.
Eleventh step for Learning C++ Programming
When your program crashes
Summary Two basic concepts: variables and assignments Basic types:
Exceptions 1 CMSC 202.
By Hector M Lugo-Cordero September 3, 2008
Object-Oriented Programming (OOP) Lecture No. 43
Pointers & Functions.
(Dreaded) Quiz 2 Next Monday.
Object-Oriented Programming (OOP) Lecture No. 44
CS31 Discussion 1D Fall18: week 2
Lecture 9.
Exceptions for safe programming.
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

Difference from Java Fewer exceptions No null pointer exception No divide by zero exception Undefined behavior instead No finally block (try, catch, finally) Catch all statements Standard: inherit from std::exception or std::runtime_error (for what) Use catch (...) for all Use catch(const char* Message) for a string No concept of checked exceptions

See no divide by zero error #include <iostream> #include <string> using namespace std; int main(void) { int pencils, erasers; double ppe; //pencils per eraser cout << "How many pencils do you" << " have?\n"; cin >> pencils; cout << "How many erasers do you" << " have?\n"; cin >> erasers; ppe = pencils / (double) erasers; cout << "Each eraser must last through " << ppe << " pencils." << endl; cout << "End of program." << endl; return(0); }

Just Throw a String #include <iostream> #include <string> using namespace std; int main (void) { int pencils, erasers; double ppe; //pencils per eraser cout << "How many pencils do you" << " have?\n"; cin >> pencils; cout << "How many erasers do you" << " have?\n"; cin >> erasers; try if (erasers == 0) throw "divided by zero"; } ppe = pencils / (double) erasers; cout << "Each eraser must last through " << ppe << " pencils." << endl ; cout << "End of program." << endl; catch (const char *Message) cout << Message << endl; return (0);

Or throw object of Existing Class #include <stdexcept> #include <iostream> #include <string> using namespace std; int main (void) { int pencils, erasers; double ppe; //pencils per eraser cout << "How many pencils do you" << " have?\n"; cin >> pencils; cout << "How many erasers do you" << " have?\n"; cin >> erasers; try if (erasers == 0) throw runtime_error("a message"); } ppe = pencils / (double) erasers; cout << "Each eraser must last through " << ppe << " pencils." << endl; cout << "End of program." << endl; catch (runtime_error e) cout << e.what() << endl; return (0);

Throw an error object you create Step 1: Create an error class Create a small class named as the error type Empty or filled is fine using namespace std; class NegativeNumber { public: NegativeNumber(void) {} NegativeNumber(string theMessage) : message(theMessage) {} string getMessage() const {return message; } private: string message; }; class DivideByZero {};

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

Throw an error object you create 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.

int main(void) { int pencils, erasers; double ppe; //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 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);

Summary At error – throw exception At use of the method: String Existing exception Your own class empty Your own class with data At use of the method: Try { } surrounding all that should not be done for error Catch ( ) { } do only if error If standard error, use what method If custom use your method … to catch all