Object Oriented Programming COP3330 / CGS5409

Slides:



Advertisements
Similar presentations
Pearson Education, Inc. All rights reserved. 1.. Exception Handling.
Advertisements

 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Exception Handling Outline 13.1 Introduction 13.2 Exception-Handling Overview 13.3 Other.
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.
Review Linked list: Doubly linked list, insertback, insertbefore Remove Search.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
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.
Java Structure import java_packages; class JavaClass { member variables declarations; void otherMethod( ) { } public static void main(String[]
Week 1 Algorithmization and Programming Languages.
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.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
Bitwise Operators Fall 2008 Dr. David A. Gaitros
HANDLING EXCEPTIONS Chapter 9. Outline  Learn about the limitations of traditional error-handling methods  Throw exceptions  Use try blocks  Catch.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Exception Handling Fall 2008 Dr. David A. Gaitros
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
LECTURE LECTURE 14 Exception Handling Textbook p
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
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.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
Lecture 18B Exception Handling and Richard Gesick.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Object-Oriented Programming in C++ Exception.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Chapter 1: Introduction to Computers and Programming
Bill Tucker Austin Community College COSC 1315
Chapter 1.2 Introduction to C++ Programming
Exception Handling C++.
Exceptions Error Handling and Recovery
16 Exception Handling.
Jim Fawcett CSE687-OnLine – Object Oriented Design Summer 2017
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.
LESSON 06.
Andy Wang Object Oriented Programming in C++ COP 3330
Jim Fawcett CSE687 – Object Oriented Design Spring 2001
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Chapter 1: Introduction to computers and C++ Programming
Chapter 1.2 Introduction to C++ Programming
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 2: Introduction to C++
Jim Fawcett CSE687 – Object Oriented Design Spring 2015
Data types Data types Basic types
Why exception handling in C++?
EGR 2261 Unit 4 Control Structures I: Selection
Part IX Fundamentals of C and C++ Programming Exception Handling
EXCEPTION HANDLING.
Chapter 1: Introduction to Computers and Programming
Exception Handling and
Exceptions with Functions
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 17 Templates and Exceptions Part 2
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 7 Additional Control Structures
Exceptions Problems in a Java program may cause exceptions or errors representing unusual or invalid processing. An exception is an object that defines.
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 3: Input/Output
Exception Handling.
By Hector M Lugo-Cordero September 3, 2008
Chapter 2: Introduction to C++.
Programs written in C and C++ can run on many different computers
What Actions Do We Have Part 1
Capitolo 1 – Introduction C++ Programming
Department of Computer and Information Science, School of Science, IUPUI Exception Handling Dale Roberts, Lecturer Computer Science, IUPUI
CMSC 202 Exceptions.
ENERGY 211 / CME 211 Lecture 24 November 14, 2008.
Chapter 1: Introduction to Computers and Programming
Presentation transcript:

Object Oriented Programming COP3330 / CGS5409 Recitation Week 12 Object Oriented Programming COP3330 / CGS5409

Today’s Recitation Exception Handling Bitwise Operators

Exception Handling Many erroneous situations could occur during program execution Usually related to things like user input Exception Handling is a type of error- checking, available in many programming languages

Exception Handling Exception - some sort of problem or error that occurs during a program's execution Exception handler - a piece of code that resolves an exception situation Typical error-checking often intermixed with the tasks of a program (if-statements, etc) Exception handlers are intended to be separate from the main tasks

Why Exception Handling? Exception handling can improve a program's fault tolerance Exception handlers are separate from main tasks of a program Can improve readability and modifiability

Why Exception Handling? This doesn't mean that exception handlers should be used in all cases! Sometimes conventional error-checking is more appropriate Exception handling best for problems that occur infrequently

When to use Exception Handlers Exception handling is good for situations in which the error doesn't need to be handled in the same block in which it occurred Errors that will result in termination of the program, for instance, would fall into this category

Using Exception Handlers Reserved words in C++: try, throw, catch try blocks Consists of keyword try and a block of code { } inside set braces Encloses the statements that might cause exceptions catch blocks One or more catch blocks follow a try block. (also code enclosed in { } set braces) Each catch block is an exception handler A catch block has a single parameter (with type listed)

Using Exception Handlers If an exception occurs in a try block The try block immediately ends The program attempts to match the exception to one of the catch handlers (based on type of item thrown) If a match is found, the code in the catch block executes Maximum of one catch block will be matched, if any Program control resumes after the last catch block If no exceptions occur in a try block, the catch blocks are skipped!

Using Exception Handlers The point where an exception occurs is called the throw point Keyword throw used to "throw" a specific kind of exception to be caught In C++, there is a standard library with pre-built exception classes. The primary base class is called exception, and comes from here: #include <exception> using std::exception;

Simple Exception Example #include <iostream> using namespace std; int main() { int cookies, people; double cpp; try { cout << "Enter number of people: "; cin >> people; cout << "Enter number of cookies: "; cin >> cookies; if (cookies == 0) throw people; else if (cookies < 0) throw static_cast<double>(people);

Simple Exception Example cont. cpp = cookies/static_cast<double>(people); cout << cookies << " cookies.\n" << people << " people.\n" << "You have " << cpp << " cookies per person.\n"; } catch(int e) { cout << e << " people, and no cookies!\nGo buy some cookies!\n"; catch(double t) cout << "Second catch block type double -- do we reach it?\n"; cout << "End of program.\n"; return 0;

Bitwise Operators Memory is made up of bits and bytes A bit is the smallest unit of storage in a computer. It stores a 0 or a 1. A byte consists of 8 bits, and is special because it is usually the smallest unit of directly addressable storage. This means - it is the smallest item that we can create a variable out of. Addresses in memory are usually applied to bytes.

Bitwise Operators The smallest built-in data type is the char, which on most systems today is 1 byte. So... what if we want to access individual bits? Is this possible? Yes -- but not directly. We must use the bitwise operators to act at the bit level. Caution: Bitwise operations may be machine- dependent! (Big/Little ENDIAN)

Bitwise Operators Operator Name Arity Description & Bitwise AND Binary Similar to the && operator, but on a bit-by-bit basis.  Bits in the result set to 1 if corresponding operand bits are both 1, and set to 0 otherwise | Bitwise OR (inclusive) Similar to the || operator, but on a bit-by-bit basis.  Bits in the result set to 1 if at least one of the corresponding bits in the two operands is 1.  0 otherwise. ^ Bitwise OR (exclusive) Shifts the bits of the first operand to the left, by the number of bits specified in the second operand. Right fill with 0 bits.

Bitwise Operators Operator Name Arity Description << Left Shift Binary Shifts the bits of the first operand to the left, by the number of bits specified in the second operand. Right fill with 0 bits. >> Right Shift Shifts the bits of the first operand to the right, by the number of bits specified in the second operand. Left fill depends on the machine. Usually based on the sign (fill with 0's for positive numbers, 1's for negatives). ~ Complement Unary Flips the bits in the operand. Similar to negation. (All 1's become 0's, and all 0's become 1's).

Bitwise Operators Also, there are shortcut assignment operators, similar to arithmetic operators: x &= y means x = x & y x |= y means x = x | y x ^= y means x = x ^ y x <<= y means x = x << y x >>= y means x = x >> y

Bitwise AND Example (x & y) Suppose we have the following code: short x = 6891; short y = 11318; And let's assume that we are using a machine in which a short is 2 bytes (which is 16 bits). The binary representations of x and y, then, are: x: 00011010 11101011 y: 00101100 00110110 Suppose we did the operation (x & y). Then we perform the AND operation on the individual bits: -------------------------- result: 00001000 00100010 // this is the value 2082

Bitwise OR Examples (x | y) (x ^y) Suppose we have the following code: short x = 6891; short y = 11318; Bitwise OR operation (x | y): x: 00011010 11101011 y: 00101100 00110110 -------------------------- result: 00111110 11111111 // this is the value 16127 Bitwise exclusive OR operation (x ^ y): result: 00110110 11011101 // this is the value 14045

Bitwise Shift & Complement Examples >>, <<, ~ Here is a bitwise left shift, performed on x (x << 2): x: 00011010 11101011 --------------------------- shifted: 01101011 10101100 // this is the value 27564 Here is a bitwise right shift, performed on y (y >> 4): y: 00101100 00110110 shifted: 00000010 11000011 // this is the value 707 And here is the complement of x (~x) ~x: 11100101 00010100 // this is the value -6892

Bitwise Code Example #include <iostream> #include <iomanip> using std::cout; using std::endl; int main() { short x = 6891; short y = 11318; cout << "x = " << x << "\ny = " << y << endl; cout << "x & y = " << (x & y) << endl; cout << "x | y = " << (x | y) << endl; cout << "x ^ y = " << (x ^ y) << endl; cout << "x << 2 = " << (x << 2) << endl; cout << "y >> 4 = " << (y >> 4) << endl; cout << "~x = " << ~x << endl; }

Questions?