Chapter 20 C Namespace and Exceptions - PowerPoint PPT Presentation

About This Presentation
Title:

Chapter 20 C Namespace and Exceptions

Description:

The names are unique in their scope. Can be defined more than once, ... Handle unusual situation so they will be. executed normally ... – PowerPoint PPT presentation

Number of Views:13
Avg rating:3.0/5.0
Slides: 16
Provided by: radf7
Category:

less

Transcript and Presenter's Notes

Title: Chapter 20 C Namespace and Exceptions


1
Chapter 20 C Namespace and Exceptions
  • By C. Shing
  • ITEC Dept
  • Radford University

2
Objectives
  • Understand how to create namespace
  • Understand how to throw exception
  • Understand how to handle exception

3
Namespace
  • Contains unique names which are defined
  • in the space
  • The names are unique in their scope
  • Can be defined more than once,
  • such as define the specification once and
  • define the implementation the second time

4
Namespace (Cont.)
  • Form
  • define
  • namespace yournamespace
  • class specification/implementation
  • Invoke the names
  • using namespace yournamespace

5
Example
  • Namespaces for using data structures
  • linked list, stack, queue and tree
  • name.h

6
Example (Cont.)
  • Use namespaces for data structures
  • linked list, stack, queue and tree
  • uselist.cpp
  • usestack.cpp
  • usequeue.cpp
  • usetree.cpp

7
Exception
  • An unusual situation that makes the code
  • not working as normal
  • Handle unusual situation so they will be
  • executed normally
  • If exception not handled, then system will
  • execute terminate to end the execution of
  • the program

8
Exception (Cont.)
  • Form
  • try
  • exeception could occur here
  • catch (exception type1)
  • exeception handled here
  • catch (exception type2)
  • exeception handled here

9
Exception (Cont.)
  • If exception happens in try block, the code ended
  • at throw line. Then control moved to catch block
  • and finished in the block.
  • If no exception happen in try block, then control
  • will move to the statement after the catch
    block.
  • When catch exception, it will start from the
  • 1st catch block down one by one until it finds
    the
  • corresponding exception type

10
Handle Exception
  1. Use library routines
  2. Use user defined exeception

11
Handle Exception library exception
  • Example
  • bubbleclass_template.cpp
  • bubbleclasstemplate_data.txt

12
Handle Exception user-defined exception
  • Write function that throw exception
  • Can have any possible exception type
  • returntype functionname ()
  • if () throw exception1
  • Only exception1, exception2, listed are
    possible
  • returntype functionname ()
  • throw exception1, exception2,
  • if () throw exception1

13
Handle Exception user-defined exception (Cont.)
  • Write function that throw exception (Cont.)
  • If not in case 1 or 2, then the system will
  • terminate abnormally

14
Handle Exception user-defined exception (Cont.)
  • The exception can be a class and arranged
  • in inheritance hierarchy
  • superclass must throw all the exceptions that
  • all subclasses have thrown
  • If the exception is not caught in the subclass
    that
  • was thrown, it must be caught from the bottom of
  • the hierarchy up.

15
References
  • Deitel Deitel C How to Program, 4th ed.,
  • Chapter 23, Prentice Hall
Write a Comment
User Comments (0)
About PowerShow.com