Lecture 12  Namespaces  Exceptions  Type casting  Pre-processor directives.

Slides:



Advertisements
Similar presentations
Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
Advertisements

Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
Exception Handling. Background In a perfect world, users would never enter data in the wrong form, files they choose to open would always exist, and code.
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.
 2006 Pearson Education, Inc. All rights reserved. Exception Handling in C++ CS-2303, C-Term Exception Handling in C++ CS-2303 System Programming.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Exception Handling: A Deeper.
C++ Exception Handling
1 CSC241: Object Oriented Programming Lecture No 28.
Exception Handling Topics We will discuss the following main topics: – Handling Exceptions – Throwing Exceptions – More about Input/Output Streams.
CS Advanced C++ Exception Handling Topic #5.
CS 4800 By Brandon Andrews.  Specifications  Goals  Applications  Design Steps  Testing.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Exception Handling An Exception is an indication of a problem that occurs during a program’s execution. Exception handling enables the programmer to create.
 2006 Pearson Education, Inc. All rights reserved Exception Handling.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
You gotta be cool. C++ ? C++ Standard Library Header Files Inline Functions References and Reference Parameters Default Arguments and Empty Parameter.
1 CSC103: Introduction to Computer and Programming Lecture No 26.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Dale Roberts Exception Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science,
Exception Handling 1. Introduction Users may use our programs in an unexpected ways. Due to design errors or coding errors, our programs may fail in unexpected.
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
1 CSC241: Object Oriented Programming Lecture No 27.
COMPUTER PROGRAMMING 2 Exceptions. What are Exceptions? Unexpected events that happen when the code is executing (during runtime). Exceptions are types.
Exception Handling. 2 Two types of bugs (errors) Logical error Syntactic error Logical error occur  Due to poor understanding of the problem and solution.
Object Oriented Programming Elhanan Borenstein Lecture #4.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
Exceptions Handling Exceptionally Sticky Problems.
Exception. Runtime Error Consider the following program: public class BadArray { public static void main(String[] args) { // Create an array with three.
Introduction to Exception Handling and Defensive Programming.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
1 Preprocessor –How it works File Inclusion: #include Macro Definition: #define Predefined macros –__LINE__, __FILE__, __DATE__, __TIME__ Conditional Compilation.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessing Lecture 12 April 7, 2005.
HANDLING EXCEPTIONS Chapter 9. Outline  Learn about the limitations of traditional error-handling methods  Throw exceptions  Use try blocks  Catch.
CS212: Object Oriented Analysis and Design Lecture 20: Exception Handling-II.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
Exception Handling in C++. Outline What exceptions are and when to use them Using try, catch and throw to detect, handle and indicate exceptions, respectively.
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
CSCI 383 Object-Oriented Programming & Design Lecture 20 Martin van Bommel.
LECTURE LECTURE 14 Exception Handling Textbook p
THE PREPROCESSOR
C PREPROCESSOR. Introduction  It is a program that processes our source program before it is passed to the compiler.  Preprocessor commands (often known.
Exceptions Lecture 11 COMP 401, Fall /25/2014.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
C++ Namespaces, Exceptions CSci 588: Data Structures, Algorithms and Software Design All material not from online sources copyright © Travis Desell, 2011.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
1 n Object Oriented Programming. 2 Introduction n procedure-oriented programming consists of writing a list of instructions and organizing these instructions.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
C++ Lesson 1.
C ++ MULTIPLE CHOICE QUESTION
Exception Handling in C++
IS 0020 Program Design and Software Tools
16 Exception Handling.
Jim Fawcett CSE687-OnLine – Object Oriented Design Summer 2017
CS212: Object Oriented Analysis and Design
Jim Fawcett CSE687 – Object Oriented Design Spring 2001
Jim Fawcett CSE687 – Object Oriented Design Spring 2015
Chapter 13 - The Preprocessor
Indexer AKEEL AHMED.
Chapter 11—Exceptions Handling Exceptions Throwing Exceptions.
C Preprocessor(CPP).
16 Exception Handling.
ENERGY 211 / CME 211 Lecture 24 November 14, 2008.
Conditional Compilation
Presentation transcript:

Lecture 12  Namespaces  Exceptions  Type casting  Pre-processor directives

Namespaces  :: – The scope operator allows us to access elements within a namespace or known class/struct/enum  A namespace can encapsulate functions, classes, structs and enums  A namespace allows elements with the same name to exist  Anything not in a namespace is part of the global namespace

Exceptions  Exceptions can be used to catch runtime errors  Exceptions are held within a try {} catch(int e) {} block.  The catch block can be used to identify the exception that occurs through a function argument  (…) ellipsis can be used to catch any exception (default exception handling  The catch value is thrown using the throw keyword

Exceptions cont.  The C++ standard library provides an exception base class using #include  It has a virtual function called ‘what’ which returns a char*  This class can be used to throw detailed exception messages defined in the derived classes

Standard exceptions exceptiondescription bad_alloc thrown by new on allocation failure bad_cast thrown by dynamic_cast when it fails in a dynamic cast bad_exception thrown by certain dynamic exception specifiers bad_typeid thrown by typeid bad_function_callthrown by empty function objects bad_weak_ptr thrown by shared_ptr when passed a bad weak_ptr

Basic exceptions

Using standard lib exceptions

Custom exceptions

Preprocessor  Preceded by a #  There is no end line statement (;)  Can only extend through multiple lines using a backslash  #define fact(f,n) for (f=1; (n); (n)--)\ f*=n;  Is examined before compilation

Preprocessor  #define – defines a name as a value  #undef – removes that definition  #ifdef IDENTIFIER_NAME – If it is defined this code can be compiled  #ifndef – If the identifier is not defined, run this code  #if #elif  #error used to throw a compile error

Preprocessor cont.  #include – used to include files  #line [“filename”] #line 13 “myfile.txt”  #pragma – compiler specific macros, GCC generally avoids #pragma directives but includes some for general compatibility

Always defined macros  __FILE__ – name of source file being compiled  __DATE__ Mmm dd yyyy of compilation start  __TIME__ hh:mm:ss time of compilation start  __cplusplus

Technical demo 9/10 th of April Course representation Ball (moving) Ball wall collisions Ball obstacle collisions Ball hole collision Putting stick/indicator Putting stick interaction with ball Software engineering Data structures use OpenGL feature use Variations on standard behaviour GUI Extras