Recitation Week 12 - PowerPoint PPT Presentation

About This Presentation
Title:

Recitation Week 12

Description:

Object Oriented Programming COP3330 / CGS5409 – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 23
Provided by: Michael2936
Learn more at: https://ww2.cs.fsu.edu
Category:

less

Transcript and Presenter's Notes

Title: Recitation Week 12


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

2
Todays Recitation
  • Exception Handling
  • Bitwise Operators

3
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

4
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

5
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

6
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

7
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

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

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

10
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 ltexceptiongt
  • using stdexception

11
Simple Exception Example
  • include ltiostreamgt
  • using namespace std
  • int main()
  • int cookies, people
  • double cpp
  • try
  • cout ltlt "Enter number of people "
  • cin gtgt people
  • cout ltlt "Enter number of cookies "
  • cin gtgt cookies
  • if (cookies 0)
  • throw people
  • else if (cookies lt 0)
  • throw static_castltdoublegt(people)

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

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

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

15
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) 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 operands 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.
16
Bitwise Operators
Operator Name Arity Description
ltlt 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.
gtgt 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
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 ltlt y means x x ltlt y
  • x gtgt y means x x gtgt y

18
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
  • x 00011010 11101011
  • y 00101100 00110110
  • --------------------------
  • result 00001000 00100010 // this is the
    value 2082

19
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)
  • x 00011010 11101011
  • y 00101100 00110110
  • --------------------------
  • result 00110110 11011101 // this is the
    value 14045

20
Bitwise Shift Complement Examples gtgt, ltlt,
  • Here is a bitwise left shift, performed on x (x
    ltlt 2)
  • x 00011010 11101011
  • ---------------------------
  • shifted 01101011 10101100 // this is the
    value 27564
  • Here is a bitwise right shift, performed on y (y
    gtgt 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
Bitwise Code Example
  • include ltiostreamgt
  • include ltiomanipgt
  • using stdcout
  • using stdendl
  • int main()
  • short x 6891
  • short y 11318
  • cout ltlt "x " ltlt x ltlt "\ny " ltlt y ltlt endl
  • cout ltlt "x y " ltlt (x y) ltlt endl
  • cout ltlt "x y " ltlt (x y) ltlt endl
  • cout ltlt "x y " ltlt (x y) ltlt endl
  • cout ltlt "x ltlt 2 " ltlt (x ltlt 2) ltlt endl
  • cout ltlt "y gtgt 4 " ltlt (y gtgt 4) ltlt endl
  • cout ltlt "x " ltlt x ltlt endl

22
Questions?
Write a Comment
User Comments (0)
About PowerShow.com