object oriented programming cop3330 cgs5409 exception
play

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-


  1. Object Oriented Programming COP3330 / CGS5409

  2.  Exception Handling  Bitwise Operators

  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

  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

  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

  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 occur infrequently

  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

  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)

  9.  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!

  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;

  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);

  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; }

  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.

  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)

  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 Binary Similar to the || operator, but on a (inclusive) 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 Binary Shifts the bits of the first operand to (exclusive) the left, by the number of bits specified in the second operand. Right fill with 0 bits.

  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).

  17.  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

  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  operation on the individual bits: x: 00011010 11101011 y: 00101100 00110110 -------------------------- result: 00001000 00100010 // this is the value 2082

  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

  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

  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; }

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend