SLIDE 1
Object Oriented Programming COP3330 / CGS5409 Exception Handling - - PowerPoint PPT Presentation
Object Oriented Programming COP3330 / CGS5409 Exception Handling - - PowerPoint PPT Presentation
Object Oriented Programming COP3330 / CGS5409 Exception Handling Bitwise Operators Many erroneous situations could occur during program execution Usually related to things like user input Exception Handling is a type of error-
SLIDE 2
SLIDE 3
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
SLIDE 4
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
SLIDE 5
Exception handling can improve a program's
fault tolerance
Exception handlers are separate from main
tasks of a program
Can improve readability and modifiability
SLIDE 6
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
- ccur infrequently
SLIDE 7
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
SLIDE 8
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)
SLIDE 9
If an exception occurs in a try block
- The try block immediately ends
- The program attempts to match the exception to
- ne 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!
SLIDE 10
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;
SLIDE 11
#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);
SLIDE 12
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; }
SLIDE 13
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.
SLIDE 14
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)
SLIDE 15
Operator Name Operator Name Arity Arity Description 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) Binary 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
- perands is 1. 0 otherwise.
^ Bitwise OR (exclusive) 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.
SLIDE 16
Operator Name Operator Name Arity Arity Description 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 Binary 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).
SLIDE 17
Also, there are shortcut assignment
- perators, 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
SLIDE 18
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
- peration on the individual bits:
x: 00011010 11101011 y: 00101100 00110110
- result: 00001000 00100010 // this is the value 2082
SLIDE 19
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):
x: 00011010 11101011 y: 00101100 00110110
- result: 00110110 11011101 // this is the value 14045
SLIDE 20
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: 00011010 11101011
- ~x: 11100101 00010100 // this is the value -6892
SLIDE 21
#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; }
SLIDE 22